Dart/Flutter – How to get keys and values from Map

In this post we will create an example of how to get keys and values from a map, using a few approaches.

Suppose we have a map like below:

Map<String, String> map = {
  'name': 'Phuc Tran',
  'email': 'phuc.tran@nextfunc.com',
  'website': 'https://coflutter.com'
};

1. Get all keys from Map

void getKeysFromMap(Map map) {
  print('----------');
  print('Get keys:');
  // Get all keys
  map.keys.forEach((key) {
    print(key);
  });
}

// Output
 Get keys:
 name
 email
 website

2. Get all values from Map

void getValuesFromMap(Map map) {
  // Get all values
  print('----------');
  print('Get values:');
  map.values.forEach((value) {
    print(value);
  });
}

// Output
 Get values:
 Phuc Tran
 phuc.tran@nextfunc.com
 https://coflutter.com

3. Get all keys and values using forEach method

void getKeysAndValuesUsingForEach(Map map) {
  // Get all keys and values at the same time using map.forEach
  print('----------');
  print('Get keys and values using map.forEach:');
  map.forEach((key, value) {
    print('Key = $key : Value = $value');
  });
}

// Output
 Get keys and values using map.forEach:
 Key = name : Value = Phuc Tran
 Key = email : Value = phuc.tran@nextfunc.com
 Key = website : Value = https://coflutter.com

4. Get all keys and values from Map using entries

void getKeysAndValuesUsingEntries(Map map) {
  // Get all keys and values at the same time using entries
  print('----------');
  print('Get keys and values using entries:');
  map.entries.forEach((entry) {
    print('Key = ${entry.key} : Value = ${entry.value}');
  });
}

// Output
 Get keys and values using entries:
 Key = name : Value = Phuc Tran
 Key = email : Value = phuc.tran@nextfunc.com
 Key = website : Value = https://coflutter.com

Full example (Github link)

void main() {
  Map<String, String> map = {
    'name': 'Phuc Tran',
    'email': 'phuc.tran@nextfunc.com',
    'website': 'https://coflutter.com'
  };

  getKeysFromMap(map);
  getValuesFromMap(map);
  getKeysAndValuesUsingForEach(map);
  getKeysAndValuesUsingEntries(map);
}

void getKeysFromMap(Map map) {
  print('----------');
  print('Get keys:');
  // Get all keys
  map.keys.forEach((key) {
    print(key);
  });
}

void getValuesFromMap(Map map) {
  // Get all values
  print('----------');
  print('Get values:');
  map.values.forEach((value) {
    print(value);
  });
}

void getKeysAndValuesUsingForEach(Map map) {
  // Get all keys and values at the same time using map.forEach
  print('----------');
  print('Get keys and values using map.forEach:');
  map.forEach((key, value) {
    print('Key = $key : Value = $value');
  });
}

void getKeysAndValuesUsingEntries(Map map) {
  // Get all keys and values at the same time using entries
  print('----------');
  print('Get keys and values using entries:');
  map.entries.forEach((entry) {
    print('Key = ${entry.key} : Value = ${entry.value}');
  });
}

You may want to read

Dart – How to loop/iterate a list

Dart – How to find an item in a list

Tagged : / / / /
Subscribe
Notify of
guest

5 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
floorzymakeover
1 year ago

Nice blog. Thank you so much.

keerthy
1 year ago

Very nice blog. Thanks for your informative blogs.

keerthy
1 year ago

Very nice blog. Thank you for sharing this type of informative blog.

keerthy
1 year ago

Thank you so much for this amazing information! I am so happy with your blog .This is an excellent piece of tips. I just read your blog. Thank you very much for this blog

Rajitha
2 years ago

Thank you for sharing this useful information.

5
0
Would love your thoughts, please comment.x
()
x