如何在flutter中定义map <>

时间:2019-12-27 09:34:20

标签: flutter dart

我不知道如何在Flutter中使用地图。我也尝试了文档,但是它不起作用。我想在Flutter中定义一个地图,并使用一个函数将<k,v>从其他类添加到地图。有人告诉我声明一个全球地图,但是如果您还有其他内容,请帮帮我。

class Global extends StatelessWidget {
  var  myMap = {
    "one": "asd",
    "two": "asd2",
    "three": "aad3",
  };

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    throw UnimplementedError();
  }
}

3 个答案:

答案 0 :(得分:0)

使用此

Map<String, String> someMap = {
    "one": "asd",
    "two": "asd2",
    "three": "aad3",
  };

代替这个

 var  myMap = {
    "one": "asd",
    "two": "asd2",
    "three": "aad3",
  };

答案 1 :(得分:0)

为地图增值的示例代码

`void main() {
  var myMap = {
    "id": "jay",
    "password": "1234",
    "name": "Jay Tillu",
  };
  print("************ Before adding data in Map ************");
  print(myMap);

  // Adding value to Map

  myMap["country"] = "india";
  print("************ After adding data in Map ************");
  print(myMap);
}`

答案 2 :(得分:0)

var details = {'Username':'tom','Password':'pass@123'}; 
print(details);

var details = new Map(); 
details['Username'] = 'admin'; 
details['Password'] = 'admin@123';
details.forEach((k,v) => print('${k}: ${v}')); //forEach enables iterating through the Map’s entries.