Dart语言学习Dart Map类型

Posted jukaiit

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dart语言学习Dart Map类型相关的知识,希望对你有一定的参考价值。

Map创建

创建Map: var map1 = {"first":"Dart",1:true,true:"2"};
创建不可变Map: var map2 = const{"first":"Dart",1:true,true:"2"};

构造创建:var map3 = new Map();

常用操作

[],length,keys,values,
containsKey,
containsValue,
remove,forEach 

  var map1 = {"first":"Dart",1:true,true:"2"};
  print(map1);

  print(map1["first"]);
  print(map1[true]);
  map1[1] = false;
  print(map1);

  var map2 = const {1:"Dart",2:"Java"};
//  map2[1] = "Python"; //Unsupported operation: Cannot set value in unmodifiable Map

  var map3 = new Map();
  print(map3);

  var map = {"first":"Dart","second":"Java","third":"Python"};
  print(map.length);
//  map.isEmpty;

  print(map.keys);
  print(map.values);

  print(map.containsKey("first"));
  print(map.containsValue("C"));

  map.remove("third");
  print(map);

  map.forEach(f);

  var list = ["1","2","3"];
  print(list.asMap());

输出:

{first: Dart, 1: true, true: 2}
Dart
2
{first: Dart, 1: false, true: 2}
{}
3
(first, second, third)
(Dart, Java, Python)
true
false
{first: Dart, second: Java}
key=first,value=Dart
key=second,value=Java
{0: 1, 1: 2, 2: 3}

 





以上是关于Dart语言学习Dart Map类型的主要内容,如果未能解决你的问题,请参考以下文章

Flutter学习-Dart语法

Flutter入门Dart语言:初步了解内建类型

Dart 入门教程 Dart 变量常量数据类型以及一些规范

Dart 入门教程 Dart 变量常量数据类型以及一些规范

Dart语言--基础内容

Flutter/Dart - 集合类型List、Map详解