CuriousCoder
CuriousCoder

Reputation: 262

How To Create List From Map Key

I have a map

Map<String, List<String>> myMap = {
            'Example1': ['A', 'B', 'C', 'D'],
            'Example2': ['E', 'F', 'G', 'H'],
            'Example3': ['I', 'J', 'K', 'L'],
            'Example4': ['M', 'N', 'O', 'P']
          };

How can I create a List from this Map that only takes the values from just ONE of the keys? For example, how can I create the following List from the 'Example2' key from the Map above:

List <String> Example2 = ['E', 'F', 'G', 'H'];

Upvotes: 0

Views: 364

Answers (2)

encubos
encubos

Reputation: 3313

If you want to access by index, you can try to do this.

// convert to list
var _list = myMap.values.toList();

// extract using index
var values1 = _list[1];

print(values1);

Upvotes: 0

Ammar Hussein
Ammar Hussein

Reputation: 6044

you can get the list from the map by using myMap['Example2']

for example:

Map<String, List<String>> myMap = {
            'Example1': ['A', 'B', 'C', 'D'],
            'Example2': ['E', 'F', 'G', 'H'],
            'Example3': ['I', 'J', 'K', 'L'],
            'Example4': ['M', 'N', 'O', 'P']
          };
List<String> Example2 = myMap['Example2'];

Upvotes: 0

Related Questions