且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

【Flutter】Dart 数据类型 Map 类型 ( 创建 Map 集合 | 初始化 Map 集合 | 遍历 Map 集合 )

更新时间:2022-01-31 07:16:13

文章目录

一、 Dart 数据类型 Map 类型

二、 Map 类型初始化并赋值

1、 创建 Map 对象同时进行初始化操作

2、 先创建 Map 对象再进行赋值

三、 Map 集合遍历

1、 使用 forEach 遍历 Map 集合

2、 使用普通 for 循环遍历 Map 集合

3、 使用 map 方法进行遍历生成新的 Map 集合

四、 完整代码示例

五、 相关资源





一、 Dart 数据类型 Map 类型


Dart 中的 Map 数据类型与 Java 类似 , 由键值对组成 , 键 Key , 值 Value ;


其中 Key 的值在 Map 中必须是唯一的 , Value 的值可以重复 ;






二、 Map 类型初始化并赋值




1、 创建 Map 对象同时进行初始化操作


创建 Map 对象同时进行初始化操作 : 通过 {} 初始化 Map 对象, 每个元素形式为 Key : Value , 每个元素的 键( Key ) 与 值 ( Value ) 之间使用 冒号 " : " 分割 , 元素与元素之间使用 逗号 " , " 分割 ;


代码示例 :


   

// 通过 {} 初始化 Map 对象, 每个元素形式为 Key : Value
    // 键( Key ) 与 值 ( Value ) 之间使用冒号 " : " 分割
    // 元素与元素之间使用逗号 " , " 分割
    Map student = {1 : "Tom", 2 : "Jerry", 3 : "Trump"};
    // 打印 Map 集合
    print(student);


执行结果 :


{1: Tom, 2: Jerry, 3: Trump}




2、 先创建 Map 对象再进行赋值


先创建 Map 对象再进行赋值 : 先创建一个空的 Map 集合 , 使用 下标 的方式为 Map 集合赋值 , 用法如下 :


// II . 先创建空的 Map 集合 , 然后再进行初始化操作
    Map president = {};
    // 为 Map 集合添加元素
    president[1] = "Bush";
    president[2] = "Obama";
    president[3] = "Trump";
    // 打印 Map 集合
    print(president);


打印结果 :


{1: Bush, 2: Obama, 3: Trump}





三、 Map 集合遍历




1、 使用 forEach 遍历 Map 集合


使用 forEach 遍历 Map 集合 :


// 1 . 使用 forEach 进行遍历
    president.forEach( (key, value){
      print("forEach 遍历 : $key : $value");
    } );


打印结果 :


forEach 遍历 : 1 : Bush
forEach 遍历 : 2 : Obama
forEach 遍历 : 3 : Trump



2、 使用普通 for 循环遍历 Map 集合


使用普通 for 循环遍历 Map 集合 :


 

// 2 . 通过 for 循环遍历 Map 集合
    // 调用 Map 对象的 keys 成员 , 返回一个由 键 Key 组成的数组
    for (var key in president.keys){
      print("for 循环遍历 : Key : $key , Value : ${president[key]}");
    }


打印结果 :


for 循环遍历 : Key : 1 , Value : Bush
for 循环遍历 : Key : 2 , Value : Obama
for 循环遍历 : Key : 3 , Value : Trump



3、 使用 map 方法进行遍历生成新的 Map 集合


使用 map 方法进行遍历生成新的 Map 集合 : 使用 map 方法 进行遍历 , 遍历过程中 生成新的 Map 集合 , 遍历后 , 会返回一个新的 Map 集合 , 传入一个回调函数 , 参数是 Map 集合中每个元素的 键值对 key 和 value , 返回值是新的 Map 集合 ;


下面的示例将 原 Map 集合中的键值对对调 , 生成一个新的 Map 集合 , 并打印新的 Map 集合中的内容 ;


   

// 3 . 使用 map 方法进行遍历
    // 遍历过程中生成新的 Map 集合
    // 遍历后 , 会返回一个新的 Map 集合
    // 传入一个回调函数 , 参数是 key value , 返回值是新的 Map 集合
    Map president2 = president.map(
            (key, value){
          // 这里将 Map 集合中的 Key 和 Value 进行颠倒 , 生成一个新的 Map 集合
          return MapEntry(value, key);
        }
    );
    // 打印新的 Map 集合
    print(president2);


打印结果 :


{Bush: 1, Obama: 2, Trump: 3}






四、 完整代码示例



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 数据类型'));
  }
  /**
   * Map 示例
   */
  mapDemo(){
 
    // I . 定义 Map 集合并初始化
    // 通过 {} 初始化 Map 数据, 每个元素形式为 Key : Value
    // 键( Key ) 与 值 ( Value ) 之间使用冒号 " : " 分割
    // 元素与元素之间使用逗号 " , " 分割
    Map student = {1 : "Tom", 2 : "Jerry", 3 : "Trump"};
    // 打印 Map 集合
    print(student);
    // II . 先创建空的 Map 集合 , 然后再进行初始化操作
    Map president = {};
    // 为 Map 集合添加元素
    president[1] = "Bush";
    president[2] = "Obama";
    president[3] = "Trump";
    // 打印 Map 集合
    print(president);
    // III . Map 集合遍历
    // 1 . 使用 forEach 进行遍历
    president.forEach( (key, value){
      print("forEach 遍历 : $key : $value");
    } );
    // 2 . 通过 for 循环遍历 Map 集合
    // 调用 Map 对象的 keys 成员 , 返回一个由 键 Key 组成的数组
    for (var key in president.keys){
      print("for 循环遍历 : Key : $key , Value : ${president[key]}");
    }
    // 3 . 使用 map 方法进行遍历
    // 遍历过程中生成新的 Map 集合
    // 遍历后 , 会返回一个新的 Map 集合
    // 传入一个回调函数 , 参数是 key value , 返回值是新的 Map 集合
    Map president2 = president.map(
            (key, value){
          // 这里将 Map 集合中的 Key 和 Value 进行颠倒 , 生成一个新的 Map 集合
          return MapEntry(value, key);
        }
    );
    // 打印新的 Map 集合
    print(president2);
  }
}



执行结果 :


{1: Tom, 2: Jerry, 3: Trump}
{1: Bush, 2: Obama, 3: Trump}
forEach 遍历 : 1 : Bush
forEach 遍历 : 2 : Obama
forEach 遍历 : 3 : Trump
for 循环遍历 : Key : 1 , Value : Bush
for 循环遍历 : Key : 2 , Value : Obama
for 循环遍历 : Key : 3 , Value : Trump
{Bush: 1, Obama: 2, Trump: 3}



【Flutter】Dart 数据类型 Map 类型 ( 创建 Map 集合 | 初始化 Map 集合 | 遍历 Map 集合 )





五、 相关资源


参考资料 :


Dart 开发者官网 : https://api.dart.dev/

Flutter 中文网 ( 非官方 , 翻译的很好 ) : https://flutterchina.club/ , http://flutter.axuer.com/docs/

Flutter 官网 : https://flutter.dev/ ( 被墙 )

官方 GitHub 地址 : https://github.com/flutter

Flutter 相关问题 : https://flutterchina.club/faq/ ( 入门阶段推荐看一遍 )


博客源码下载 :


GitHub 地址 : https://github.com/han1202012/flutter_app_hello ( 随博客进度一直更新 , 有可能没有本博客的源码 )


博客源码快照 : https://download.csdn.net/download/han1202012/15087696 ( 本篇博客的源码快照 , 可以找到本博客的源码 )