In Dart, you can create a list of dictionaries using the `List` class for the outer list and `Map` class for the inner dictionaries.
In this example,
dict1
and dict2
using the Map
class.listOfDicts
using the List
class and initialize it with the inner dictionaries.void main() {
var dict1 = {'key1': 'value1', 'key2': 'value2'};
var dict2 = {'key3': 'value3', 'key4': 'value4'};
List<Map<String, String>> listOfDicts = [dict1, dict2];
listOfDicts.forEach((dict) {
dict.forEach((key, value) {
print('\$key=\$value ');
});
print('');
});
}
key1=value1 key2=value2 key3=value3 key4=value4
In this example,
listOfDictsEmpty
using the List
class.dict3
and dict4
using the Map
class.add
method to add the inner dictionaries dict3
and dict4
to the outer list listOfDictsEmpty
.void main() {
List<Map<String, String>> listOfDictsEmpty = [];
var dict3 = {};
var dict4 = {};
listOfDictsEmpty.add(dict3);
listOfDictsEmpty.add(dict4);
listOfDictsEmpty.forEach((dict) {
dict.forEach((key, value) {
print('\$key=\$value ');
});
print('');
});
}
In this tutorial, we learned How to create a List of Dictionaries in Dart language with well detailed examples.