Dart/Flutter 如何初始化地图内的列表?

Ant*_*t D 8 dart flutter

如何初始化地图内的列表?

Map<String,Map<int,List<String>>> myMapList = Map();
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

The method '[]' was called on null.
I/flutter (16433): Receiver: null
I/flutter (16433): Tried calling: [](9)
Run Code Online (Sandbox Code Playgroud)

Ric*_*eap 12

通过在创建地图时添加元素,或者稍后通过adding 来添加元素。例如:

var myMapList = <String, Map<int, List<String>>>{
  'someKey': <int, List<String>>{
    0: <String>['foo', 'bar'],
  },
};
Run Code Online (Sandbox Code Playgroud)

或者

var m2 = <String, Map<int, List<String>>>{}; // empty map
m2['otherKey'] = <int, List<String>>{}; // add an empty map at the top level
m2['otherKey'][2] = <String>[]; // and an empty list to that map
m2['otherKey'][2].add('baz'); // and a value to that list
print(m2);
Run Code Online (Sandbox Code Playgroud)

印刷 {otherKey: {2: [baz]}}