GaganSailor
GaganSailor

Reputation: 1063

Dart:How to convert simple map into list in dart/flutter?

I want to convert a simple map to into a list currently I am having {Jack: 23, Adam: 27, Katherin: 25} and want to achieve [{ Jack, 23 }, { Adam, 27 }, { Katherin, 25 }]

Upvotes: 0

Views: 873

Answers (1)

Yauhen Sampir
Yauhen Sampir

Reputation: 2104

Here you can find an example, the key - using map.entries

class Person {
  final String name;
  final int age;

  Person(this.name, this.age);
}

void test() {
  final map = <String, int>{"Jack": 23, "Adam": 27, "Katherin": 25};

  final list = map.entries.map((entry) => Person(entry.key, entry.value)).toList();
}

If you don't want to use Person class, you can use the package with tuple https://pub.dev/packages/tuple

Upvotes: 1

Related Questions