英文:
Dart create a Map of entries from a List
问题
在创建我的DTO时,我必须将一个对象列表转换为另一个DTO的Map。该列表如下:
final List badgesList = [BadgeItem_1, BadgeItem_2, BadgeItem_3, ...]
我想直接在声明中将其转换为基本条目:Map<String, BadgeItemDto>? badges
。
我目前是这样做的:
return CardDto(
badges: badgesList!
.map(
(badgeItem) => <String, BadgeItemDto>{
badgeItem.id: BadgeItemDto.fromDomain(badgeItem)
},
)
然而,这返回的是一个 List<Map<String, BadgeItemDto>>
而不是 Map<String, BadgeItemDto>
。我如何在声明中直接创建一个具有Map作为值的单一Map?
最终结果应该如下所示:
{
'abcdefgh':
{'id': '1234', 'badgeName': 'Lorem ipsum', ...},
'ztrewq':
{'id': '6432', 'badgeName': 'Lorem ipsum', ...}
}
为了完整起见,这是BadgeItemDTO的样子:
abstract class BadgeItemDto implements _$BadgeItemDto {
const factory BadgeItemDto({
required String badgeId,
required String badgeName,
}) = _BadgeItemDto;
factory BadgeItemDto.fromJson(Map<String, dynamic> json) =>
_$BadgeItemDtoFromJson(json);
const BadgeItemDto._();
factory BadgeItemDto.fromDomain(BadgeItem badgeItem) {
return BadgeItemDto(
badgeId: badgeItem.id.getOrCrash(),
badgeName: badgeItem.badgeName.getOrCrash(),
);
}
BadgeItem toDomain() {
return BadgeItem(
id: UniqueId.fromUniqueString(badgeId),
badgeName: BadgeName(badgeName),
);
}
}
英文:
During the creation of my DTO, i have to convert a List of objects to a Map of another DTO.
The List is looking like so:
final List badgesList = [BadgeItem_1, BadgeItem_2, BadgeItem_3, ...]
I want to convert this to the primitive entry: Map<String, BadgeItemDto>? badges
directly inside the declaration.
Currenlty I did it like so:
return CardDto(
badges: badgesList!
.map(
(badgeItem) => <String, BadgeItemDto>{
badgeItem.id: BadgeItemDto.fromDomain(badgeItem)
},
)
However, this is returning a List<Map<String, BadgeItemDto>>
instead of Map<String, BadgeItemDto>
. How can I basically have a singleMap with Map as the values created directly in the declaration?
The final outcome should look like this
{'abcdefgh':
{'id': '1234', 'badgeName': 'Lorem ipsum', ...}
'ztrewq':
{'id': '6432', 'badgeName': 'Lorem ipsum', ...}
}
To be complete, this is how the BadgeItemDTO is looking:
abstract class BadgeItemDto implements _$BadgeItemDto {
const factory BadgeItemDto({
required String badgeId,
required String badgeName,
}) = _BadgeItemDto;
factory BadgeItemDto.fromJson(Map<String, dynamic> json) =>
_$BadgeItemDtoFromJson(json);
const BadgeItemDto._();
factory BadgeItemDto.fromDomain(BadgeItem badgeItem) {
return BadgeItemDto(
badgeId: badgeItem.id.getOrCrash(),
badgeName: badgeItem.badgeName.getOrCrash(),
);
}
BadgeItem toDomain() {
return BadgeItem(
id: UniqueId.fromUniqueString(badgeId),
badgeName: BadgeName(badgeName),
);
}
}
答案1
得分: 1
以下是翻译好的内容:
- 将列表转换为映射,然后使用新的Map Entries进行映射。
- 创建一个Map.fromEntries,其中对于每个元素,您都创建一个Map Entry。
英文:
You can achieve it in the following ways:
void main() {
final items = [
BadgeItem('10', 'name 1'),
BadgeItem('20', 'name 2'),
BadgeItem('30', 'name 3'),
];
final map = items.asMap().map((k, v) => MapEntry(v.id, v));
final map2 = Map.fromEntries(items.map((v) => MapEntry(v.id, v)));
print(map);
print(map2);
}
class BadgeItem {
const BadgeItem(this.id, this.name);
final String id;
final String name;
}
- Convert the List to a Map, and then map it with new Map Entries.
- Create a Map.fromEntries, where for each element you create a Map Entry.
答案2
得分: 1
Sure, here is the translated code portion:
final List badgesList = [BadgeItem_1, BadgeItem_2, BadgeItem_3, ...];
final result = <String, BadgeItemDto>{
for (final item in badgesList)
(将项目转换为字符串的某些内容) :
(将项目转换为BadgeItemDto的某些内容),
};
I've left the parts (something to make the String from item)
and (something to make the BadgeItemDto from item)
untranslated as they seem to be code-specific and would require knowledge of the programming context to provide an accurate translation.
英文:
final List badgesList = [BadgeItem_1, BadgeItem_2, BadgeItem_3, ...]
final result = <String,BadgeItemDto>{
for (final item in badgesList)
(something to make the String from item) :
(something to make the BadgeItemDto from item),
};
Just like that. Use a collection literal for-loop-element.
People are still working far too hard trying to get .map to work for them. Useful in streams, but not so much when you have all the values in hand already.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论