创建一个从列表中的条目生成的 Map。

huangapple go评论61阅读模式
英文:

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

我目前是这样做的:

  1. return CardDto(
  2. badges: badgesList!
  3. .map(
  4. (badgeItem) => <String, BadgeItemDto>{
  5. badgeItem.id: BadgeItemDto.fromDomain(badgeItem)
  6. },
  7. )

然而,这返回的是一个 List<Map<String, BadgeItemDto>> 而不是 Map<String, BadgeItemDto>。我如何在声明中直接创建一个具有Map作为值的单一Map?

最终结果应该如下所示:

  1. {
  2. 'abcdefgh':
  3. {'id': '1234', 'badgeName': 'Lorem ipsum', ...},
  4. 'ztrewq':
  5. {'id': '6432', 'badgeName': 'Lorem ipsum', ...}
  6. }

为了完整起见,这是BadgeItemDTO的样子:

  1. abstract class BadgeItemDto implements _$BadgeItemDto {
  2. const factory BadgeItemDto({
  3. required String badgeId,
  4. required String badgeName,
  5. }) = _BadgeItemDto;
  6. factory BadgeItemDto.fromJson(Map<String, dynamic> json) =>
  7. _$BadgeItemDtoFromJson(json);
  8. const BadgeItemDto._();
  9. factory BadgeItemDto.fromDomain(BadgeItem badgeItem) {
  10. return BadgeItemDto(
  11. badgeId: badgeItem.id.getOrCrash(),
  12. badgeName: badgeItem.badgeName.getOrCrash(),
  13. );
  14. }
  15. BadgeItem toDomain() {
  16. return BadgeItem(
  17. id: UniqueId.fromUniqueString(badgeId),
  18. badgeName: BadgeName(badgeName),
  19. );
  20. }
  21. }
英文:

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:

  1. return CardDto(
  2. badges: badgesList!
  3. .map(
  4. (badgeItem) => <String, BadgeItemDto>{
  5. badgeItem.id: BadgeItemDto.fromDomain(badgeItem)
  6. },
  7. )

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

  1. {'abcdefgh':
  2. {'id': '1234', 'badgeName': 'Lorem ipsum', ...}
  3. 'ztrewq':
  4. {'id': '6432', 'badgeName': 'Lorem ipsum', ...}
  5. }

To be complete, this is how the BadgeItemDTO is looking:

  1. abstract class BadgeItemDto implements _$BadgeItemDto {
  2. const factory BadgeItemDto({
  3. required String badgeId,
  4. required String badgeName,
  5. }) = _BadgeItemDto;
  6. factory BadgeItemDto.fromJson(Map<String, dynamic> json) =>
  7. _$BadgeItemDtoFromJson(json);
  8. const BadgeItemDto._();
  9. factory BadgeItemDto.fromDomain(BadgeItem badgeItem) {
  10. return BadgeItemDto(
  11. badgeId: badgeItem.id.getOrCrash(),
  12. badgeName: badgeItem.badgeName.getOrCrash(),
  13. );
  14. }
  15. BadgeItem toDomain() {
  16. return BadgeItem(
  17. id: UniqueId.fromUniqueString(badgeId),
  18. badgeName: BadgeName(badgeName),
  19. );
  20. }
  21. }

答案1

得分: 1

以下是翻译好的内容:

  1. 将列表转换为映射,然后使用新的Map Entries进行映射。
  2. 创建一个Map.fromEntries,其中对于每个元素,您都创建一个Map Entry
英文:

You can achieve it in the following ways:

  1. void main() {
  2. final items = [
  3. BadgeItem('10', 'name 1'),
  4. BadgeItem('20', 'name 2'),
  5. BadgeItem('30', 'name 3'),
  6. ];
  7. final map = items.asMap().map((k, v) => MapEntry(v.id, v));
  8. final map2 = Map.fromEntries(items.map((v) => MapEntry(v.id, v)));
  9. print(map);
  10. print(map2);
  11. }
  12. class BadgeItem {
  13. const BadgeItem(this.id, this.name);
  14. final String id;
  15. final String name;
  16. }
  1. Convert the List to a Map, and then map it with new Map Entries.
  2. Create a Map.fromEntries, where for each element you create a Map Entry.

答案2

得分: 1

Sure, here is the translated code portion:

  1. final List badgesList = [BadgeItem_1, BadgeItem_2, BadgeItem_3, ...];
  2. final result = <String, BadgeItemDto>{
  3. for (final item in badgesList)
  4. (将项目转换为字符串的某些内容) :
  5. (将项目转换为BadgeItemDto的某些内容),
  6. };

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.

英文:
  1. final List badgesList = [BadgeItem_1, BadgeItem_2, BadgeItem_3, ...]
  2. final result = &lt;String,BadgeItemDto&gt;{
  3. for (final item in badgesList)
  4. (something to make the String from item) :
  5. (something to make the BadgeItemDto from item),
  6. };

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.

huangapple
  • 本文由 发表于 2023年5月25日 20:14:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定