gpt4 book ai didi

flutter - Dart/Flutter 如何初始化 map 内的列表?

转载 作者:行者123 更新时间:2023-12-03 02:40:59 24 4
gpt4 key购买 nike

如何初始化 map 内的列表?

Map<String,Map<int,List<String>>> myMapList = Map();

我收到一个错误:
The method '[]' was called on null.
I/flutter (16433): Receiver: null
I/flutter (16433): Tried calling: [](9)

最佳答案

通过在创建 map 时添加元素,或稍后通过 add对他们。例如:

var myMapList = <String, Map<int, List<String>>>{
'someKey': <int, List<String>>{
0: <String>['foo', 'bar'],
},
};

或者
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);

版画 {otherKey: {2: [baz]}}

关于flutter - Dart/Flutter 如何初始化 map 内的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57912045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com