当前位置:网站首页>[fluent] dart data type map type (create map set | initialize map set | traverse map set)
[fluent] dart data type map type (create map set | initialize map set | traverse map set)
2022-07-02 17:14:00 【Programmer community】
List of articles
- One 、 Dart data type Map type
- Two 、 Map Type initialization and assignment
- 1、 establish Map Objects are initialized at the same time
- 2、 First create Map Object is assigned again
- 3、 ... and 、 Map A collection of traverse
- 1、 Use forEach Traverse Map aggregate
- 2、 Using normal for Loop traversal Map aggregate
- 3、 Use map Method to generate a new Map aggregate
- Four 、 Complete code example
- 5、 ... and 、 Related resources
One 、 Dart data type Map type
Dart Medium Map Data type and Java similar , It consists of key value pairs , key Key , value Value ;
among Key The value of the Map It has to be the only one , Value The value of can be repeated ;
Two 、 Map Type initialization and assignment
1、 establish Map Objects are initialized at the same time
establish Map Objects are initialized at the same time : adopt {} initialization Map object , Each element has the form Key : Value , Of each element key ( Key ) And value ( Value ) Used between The colon " : " Division , Use between elements comma " , " Division ;
Code example :
// adopt {} initialization Map object , Each element has the form Key : Value // key ( Key ) And value ( Value ) Use colons between " : " Division // Use commas between elements " , " Division Map student = {
1 : "Tom", 2 : "Jerry", 3 : "Trump"}; // Print Map aggregate print(student);Execution results :
{
1: Tom, 2: Jerry, 3: Trump}2、 First create Map Object is assigned again
First create Map Object is assigned again : First create an empty Map aggregate , Use Subscript By Map Set assignment , Usage is as follows :
// II . First create the empty Map aggregate , Then initialize Map president = {
}; // by Map Set add elements president[1] = "Bush"; president[2] = "Obama"; president[3] = "Trump"; // Print Map aggregate print(president);Print the results :
{
1: Bush, 2: Obama, 3: Trump}3、 ... and 、 Map A collection of traverse
1、 Use forEach Traverse Map aggregate
Use forEach Traverse Map aggregate :
// 1 . Use forEach Traversal president.forEach( (key, value){
print("forEach Traverse : $key : $value"); } );Print the results :
forEach Traverse : 1 : BushforEach Traverse : 2 : ObamaforEach Traverse : 3 : Trump2、 Using normal for Loop traversal Map aggregate
Using normal for Loop traversal Map aggregate :
// 2 . adopt for Loop traversal Map aggregate // call Map Object's keys member , Returns a key Key Array of components for (var key in president.keys){
print("for Loop traversal : Key : $key , Value : ${president[key]}"); }Print the results :
for Loop traversal : Key : 1 , Value : Bushfor Loop traversal : Key : 2 , Value : Obamafor Loop traversal : Key : 3 , Value : Trump3、 Use map Method to generate a new Map aggregate
Use map Method to generate a new Map aggregate : Use map Method Traversal , During traversal Generate a new Map aggregate , After traversing , Will return a new Map aggregate , Pass in a callback function , Parameter is Map Of each element in the collection Key value pair key and value , The return value is new Map aggregate ;
The following example will primary Map Key value pairs in the set are swapped , Make a new one Map aggregate , And print new Map What's in the collection ;
// 3 . Use map Method // Generate new Map aggregate // After traversing , Will return a new Map aggregate // Pass in a callback function , Parameter is key value , The return value is new Map aggregate Map president2 = president.map( (key, value){
// There will be Map In the collection Key and Value Reverse , Make a new one Map aggregate return MapEntry(value, key); } ); // Print new Map aggregate print(president2);Print the results :
{
Bush: 1, Obama: 2, Trump: 3}Four 、 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 . Definition Map Collect and initialize // adopt {} initialization Map data , Each element has the form Key : Value // key ( Key ) And value ( Value ) Use colons between " : " Division // Use commas between elements " , " Division Map student = {
1 : "Tom", 2 : "Jerry", 3 : "Trump"}; // Print Map aggregate print(student); // II . First create the empty Map aggregate , Then initialize Map president = {
}; // by Map Set add elements president[1] = "Bush"; president[2] = "Obama"; president[3] = "Trump"; // Print Map aggregate print(president); // III . Map A collection of traverse // 1 . Use forEach Traversal president.forEach( (key, value){
print("forEach Traverse : $key : $value"); } ); // 2 . adopt for Loop traversal Map aggregate // call Map Object's keys member , Returns a key Key Array of components for (var key in president.keys){
print("for Loop traversal : Key : $key , Value : ${president[key]}"); } // 3 . Use map Method // Generate new Map aggregate // After traversing , Will return a new Map aggregate // Pass in a callback function , Parameter is key value , The return value is new Map aggregate Map president2 = president.map( (key, value){
// There will be Map In the collection Key and Value Reverse , Make a new one Map aggregate return MapEntry(value, key); } ); // Print new Map aggregate print(president2); }}Execution results :
{
1: Tom, 2: Jerry, 3: Trump}{
1: Bush, 2: Obama, 3: Trump}forEach Traverse : 1 : BushforEach Traverse : 2 : ObamaforEach Traverse : 3 : Trumpfor Loop traversal : Key : 1 , Value : Bushfor Loop traversal : Key : 2 , Value : Obamafor Loop traversal : Key : 3 , Value : Trump{
Bush: 1, Obama: 2, Trump: 3}
5、 ... and 、 Related resources
Reference material :
- Dart Developer website : https://api.dart.dev/
- Flutter Chinese net ( unofficial , The translation is very good ) : https://flutterchina.club/ , http://flutter.axuer.com/docs/
- Flutter Official website : https://flutter.dev/ ( Quilt wall )
- official GitHub Address : https://github.com/flutter
- Flutter Related issues : https://flutterchina.club/faq/ ( It is recommended to watch it at the introductory stage )
Blog source download :
GitHub Address : https://github.com/han1202012/flutter_app_hello ( Keep updating with the progress of the blog , There may not be the source code of this blog )
Blog source snapshot : https://download.csdn.net/download/han1202012/15087696 ( The source code snapshot of this blog , You can find the source code of this blog )
边栏推荐
- Method of C language self defining function
- 【Leetcode】13. Roman numeral to integer
- Notice on holding a salon for young editors of scientific and Technological Journals -- the abilities and promotion strategies that young editors should have in the new era
- 对接保时捷及3PL EDI案例
- 一文看懂:数据指标体系的4大类型
- A case study of college entrance examination prediction based on multivariate time series
- 酒仙网IPO被终止:曾拟募资10亿 红杉与东方富海是股东
- Lampe respiratoire PWM
- Deep learning image data automatic annotation [easy to understand]
- Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
猜你喜欢

配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)

Soul, a social meta universe platform, rushed to Hong Kong stocks: Tencent is a shareholder with an annual revenue of 1.28 billion

How to transfer business data with BorgWarner through EDI?

福元医药上交所上市:市值105亿 胡柏藩身价超40亿

剑指 Offer 25. 合并两个排序的链表

Ap和F107数据来源及处理
![L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])](/img/4c/ddf7f8085257d0eb8766dbec251345.png)
L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])

PhD battle-11 preview | review and prospect backdoor attack and defense of neural network

剑指 Offer 24. 反转链表

Exploration of mobile application performance tools
随机推荐
GeoServer:发布PostGIS数据源
lsf基础命令
How openharmony starts FA of remote devices
社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东
2322. Remove the minimum fraction of edges from the tree (XOR and & Simulation)
Easy language ABCD sort
一文看懂:数据指标体系的4大类型
远程办公对我们的各方面影响心得 | 社区征文
[error record] error -32000 received from application: there are no running service protocol
linux下配置Mysql授权某个用户远程访问,不受ip限制
How to transfer business data with BorgWarner through EDI?
OpenHarmony如何启动远程设备的FA
P6774 [NOI2020] 时代的眼泪(分块)
R及RStudio下载安装教程(超详细)
剑指 Offer 25. 合并两个排序的链表
Executive engine module of high performance data warehouse practice based on Impala
你想要的宏基因组-微生物组知识全在这(2022.7)
MOSFET器件手册关键参数解读
Day 18 of leetcode dynamic planning introduction
一年頂十年