[Flutter] Dart data type Map type (create Map collection | Initialize Map collection | Traverse Map collection)
1. Dart data type Map type
The Map data type in Dart is similar to Java, consisting of key-value pairs, key Key, value Value;
The value of Key must be unique in the Map, and the value of Value can be repeated;
2. Map type initialization and assignment
1. Create a Map object and initialize it at the same time
Create a Map object and perform initialization operations at the same time: initialize the Map object through {}, each element is in the form of Key : Value , use a colon the key ( Key ) and the value ( Value ) of each element , and the element and the element are separated by a colon " : " Use commas " , " to separate;
Code sample :
// Initialize the Map object through {}, each element is in the form of Key : Value
// Use a colon between the key ( Key ) and the value ( Value ) " : " to separate
// use a comma between elements and elements " , " to separate the
Map student = { 1 : "Tom" , 2 : "Jerry" , 3 : "Trump" } ;
// print the Map collection
print ( student ) ;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Results of the :
{1: Tom, 2: Jerry, 3: Trump}
- 1
2. Create a Map object first and then assign it
First create a Map object and then assign it: first create an empty Map collection , and use the subscript method to assign a value to the Map collection. The usage is as follows:
// II . Create an empty Map set first, and then perform initialization
Map president = { } ;
// Add element to Map collection
president [ 1 ] = "Bush" ;
president [ 2 ] = "Obama" ;
president [ 3 ] = "Trump" ;
// print the set of Maps
print ( president ) ;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
print result:
{1: Bush, 2: Obama, 3: Trump}
- 1
Three, Map collection traversal
1. Use forEach to traverse the Map collection
Use forEach to traverse the Map collection:
// 1. Use forEach to traverse
president . forEach ( ( key , value ) {
print ( "forEach traversal: $key : $value" ) ;
} ) ;
- 1
- 2
- 3
- 4
print result:
forEach traversal : 1 : Bush
forEach traversal : 2 : Obama
forEach traversal : 3 : Trump
- 1
- 2
- 3
2. Use a normal for loop to traverse the Map collection
Use a normal for loop to iterate over the Map collection:
// 2. Traverse the Map collection through a for loop
// call the keys member of the Map object and return an array of keys
for ( var key in president . keys ) {
print ( "The for loop iterates over: Key : $key , Value : ${president[key]}" ) ;
}
- 1
- 2
- 3
- 4
- 5
print result:
for loop traversal : Key : 1 , Value : Bush
for loop traversal : Key : 2 , Value : Obama
for loop traversal : Key : 3 , Value : Trump
- 1
- 2
- 3
3. Use the map method to traverse to generate a new Map collection
Use the map method to traverse to generate a new Map set: use the map method to traverse, and generate a new Map set during the traversal process. After traversal, a new Map set will be returned, and a callback function will be passed in. The parameter is each map set in the Map set. The key-value pair of the element is key and value, and the return value is a new Map collection;
The following example swaps the key-value pairs in the original Map collection, generates a new Map collection, and prints the contents of the new Map collection;
// 3. Use the map method to traverse
// A new Map set is generated during the traversal process
// After traversal, a new Map set will be returned
// Pass in a callback function, the parameter is the key value, and the return value is the new Map Set
Map president2 = president . map (
( key , value ) {
// Here the Key and Value in the Map set are reversed to generate a new Map set
return MapEntry ( value , key ) ;
}
) ;
// print the new set of Maps
print ( president2 ) ;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
print result:
{Bush: 1, Obama: 2, Trump: 3}
- 1
4. Complete code example
import 'package:flutter/material.dart';
class DartType_Map extends StatefulWidget {
@override
_DartType_ListState createState() => _DartType_ListState();
}
class _DartType_ListState extends State<DartType_Map> {
@override
Widget build(BuildContext context) {
mapDemo ( ) ;
return Container ( child : Text ( 'Map data type' ) ) ;
}
/**
* Map example
*/
mapDemo ( ) {
// I. Define the Map collection and initialize it
// Initialize Map data through {}, each element is in the form of Key : Value
// Use a colon between the key ( Key ) and the value ( Value ) " : " to separate
// use a comma between elements and elements " , " to separate the
Map student = { 1 : "Tom" , 2 : "Jerry" , 3 : "Trump" } ;
// print the Map collection
print ( student ) ;
// II . Create an empty Map set first, and then perform initialization
Map president = { } ;
// Add element to Map collection
president [ 1 ] = "Bush" ;
president [ 2 ] = "Obama" ;
president [ 3 ] = "Trump" ;
// print the set of Maps
print ( president ) ;
// III . Map collection traversal
// 1. Use forEach to traverse
president . forEach ( ( key , value ) {
print ( "forEach traversal: $key : $value" ) ;
} ) ;
// 2. Traverse the Map collection through a for loop
// call the keys member of the Map object and return an array of keys
for ( var key in president . keys ) {
print ( "The for loop iterates over: Key : $key , Value : ${president[key]}" ) ;
}
// 3. Use the map method to traverse
// A new Map set is generated during the traversal process
// After traversal, a new Map set will be returned
// Pass in a callback function, the parameter is the key value, and the return value is the new Map Set
Map president2 = president . map (
( key , value ) {
// Here the Key and Value in the Map set are reversed to generate a new Map set
return MapEntry ( value , key ) ;
}
) ;
// print the new set of Maps
print ( president2 ) ;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
Results of the :
{ 1 : Tom , 2 : Jerry , 3 : Trump }
{ 1 : Bush , 2 : Obama , 3 : Trump }
forEach iterates over : 1 : Bush
forEach traversal : 2 : Obama
forEach traversal : 3 : Trump
for loop traversal : Key : 1 , Value : Bush
for loop traversal : Key : 2 , Value : Obama
for loop traversal : Key : 3 , Value : Trump
{ Bush : 1 , Obama : 2 , Trump : 3 }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
5. Related resources
References :
- Dart developer official website: https://api.dart.dev/
- Flutter Chinese website (unofficial, well translated): https://flutterchina.club/ , http://flutter.axuer.com/docs/
- Flutter official website: https://flutter.dev/ (by the wall)
- Official GitHub address : https://github.com/flutter
- Flutter related questions: https://flutterchina.club/faq/ (recommended to read it at the entry level)
Blog source download:
-
GitHub address: https://github.com/han1202012/flutter_app_hello (updated with the progress of the blog, there may be no source code for this blog)
-
Blog source code snapshot: https://download.csdn.net/download/han1202012/15087696 (The source code snapshot of this blog, you can find the source code of this blog)