FlutterでMap<String, dynamic>をリストに追加する方法


  1. リストの作成とMapの追加

    List<Map<String, dynamic>> myList = [];
    Map<String, dynamic> myMap = {
    "key1": "value1",
    "key2": 2,
    "key3": true,
    };
    myList.add(myMap);
  2. リストの初期化と同時にMapを追加

    List<Map<String, dynamic>> myList = [
    {
    "key1": "value1",
    "key2": 2,
    "key3": true,
    },
    ];
  3. リストの指定位置にMapを挿入

    List<Map<String, dynamic>> myList = [
    {
    "key1": "value1",
    "key2": 2,
    "key3": true,
    },
    {
    "key4": "value4",
    "key5": 5,
    "key6": false,
    },
    ];
    Map<String, dynamic> newMap = {
    "key7": "value7",
    "key8": 8,
    "key9": true,
    };
    myList.insert(1, newMap);
  4. リスト内のMapの値を更新

    List<Map<String, dynamic>> myList = [
    {
    "key1": "value1",
    "key2": 2,
    "key3": true,
    },
    ];
    myList[0]["key2"] = 3;