映射(Maps)是無序的鍵值對:
常用屬性:
keys 獲取所有的key值
values 獲取所有的value值
isEmpty 是否為空
isNotEmpty 是否不為空
常用方法:
remove(key) 刪除指定key的數據
addAll({...}) 合並映射 給映射內增加屬性
containsValue 查看映射內的值 返回true/false
forEach
map
where
any
every
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學習系列文章:https://www.cnblogs.com/jukaiit/category/1636484.html
