为什么我不能向地图数据类型添加元素(Dart / Flutter)

时间:2019-09-24 07:12:29

标签: android json dictionary flutter dart

最初,我声明了一个名为detailItem的变量,其数据类型为Map。之后,我用各种数据填充了变量,但是在更改它时会发生错误。有关更多详细信息,请参见下面包含的代码。

我尝试搜索google和文档,但仍未找到答案。

飞镖

Map detailItem;

detailItem = {
      "item": {
        'images': [],
        'title': '',
        'price': '',
        'stock': '',
        'description': '',
        'rating': 4,
        'author': {
          'name': '',
          'image': '',
          'verified': true,
        },
        'review': [

        ]
      }
    };


detailItem['item']['images'] = [
        "https://cms.dailysocial.id/wp-content/uploads/2017/12/4561bedaab1d7cef932f9d41cc091fee_game-mmorpg-iris-m-4.jpg",
        "https://cnet1.cbsistatic.com/img/-8OMdJa3DGw53_Xxdoi7mc-ZWYg=/980x551/2018/04/26/da817b98-516f-4213-9fa8-959378b900e4/pubgmobilejp.jpg"
      ];
      detailItem['item']['title'] = 'Fornite - Account LImited Edition';
      detailItem['item']['price'] = '200.993';
      detailItem['item']['stock'] = '1';
      detailItem['item']['description'] =
          'Best skins, Wukong, Love Ranger, and 4 others';
      detailItem['item']['author']['name'] = 'Zeus God Store';
      detailItem['item']['author']['image'] =
          'https://i.pinimg.com/originals/17/9c/7c/179c7c77b5808a922f64de12479d4a64.jpg';
      detailItem['item']['author']['verified'] = true;
//===== THIS IS ERROR OCCURS =====
      detailItem['item']['review'][0]['name'] = 'Adit'; //this code is where an error occurs
      detailItem['item']['review'][0]['message'] = 'Cool !';
      detailItem['item']['review'][1]['name'] = 'Julian';
      detailItem['item']['review'][1]['message'] = 'Bagus';
      detailItem['item']['review'][2]['name'] = 'Ricky';
      detailItem['item']['review'][2]['message'] = 'Keren';

我希望detailItem中变量的内容像这样:

飞镖

detailItem = {
      "item": {
        'images': [
        "https://cms.dailysocial.id/wp-content/uploads/2017/12/4561bedaab1d7cef932f9d41cc091fee_game-mmorpg-iris-m-4.jpg",
        "https://cnet1.cbsistatic.com/img/-8OMdJa3DGw53_Xxdoi7mc-ZWYg=/980x551/2018/04/26/da817b98-516f-4213-9fa8-959378b900e4/pubgmobilejp.jpg"
      ],
        'title': 'Fornite - Account LImited Edition',
        'price': '200.993',
        'stock': '1',
        'description': 'Best skins, Wukong, Love Ranger, and 4 others',
        'rating': 4,
        'author': {
          'name': 'Zeus God Store',
          'image': 'https://i.pinimg.com/originals/17/9c/7c/179c7c77b5808a922f64de12479d4a64.jpg',
          'verified': true,
        },
        'review': [
          {
             'name' : 'Adit',
             'message' : 'Cool !',
          },
          {
             'name' : 'Julian',
             'message' : 'Bagus',
          },
          {
             'name' : 'Ricky',
             'message' : 'Keren',
          }
        ]
      }
    };

但是我在代码中出现错误:

detailItem ['item'] ['review'] [0] ['name'] = 'Adit';

Error : "Invalid value: Valid value range is empty: 0"

具有地图数据类型的变量是否不能填充: x [integer] = value ??

我只想填写从API获取的JSON数据,然后将其放入变量中。

3 个答案:

答案 0 :(得分:1)

更新:您也可以做到1行

detailItem['item']['review'] = (detailItem['item']['review'] as List)
  ..addAll([{
    'name': 'Ricky',
    'message': 'Keren',
  }, {..}, {..} ]);

List<dynamic> nameMapList = detailItem['item']['review'];
nameMapList.addAll([
  {'name': 'Adit', 'message': 'Cool !'},
  {'name': 'Julian', 'message': 'Bagus'},
]);
detailItem['item']['review'] = nameMapList;
print(detailItem['item']['review']);

结果>> [{{name:Adit,message:Cool!},{name:Julian,message:Bagus}]

nameMapList.add(
  {'name': 'Ricky', 'message': 'Keren'},
);
detailItem['item']['review'] = nameMapList;
print(detailItem['item']['review']);

结果>> [{{name:Adit,message:Cool!},{name:Julian,message:Bagus},{name:Ricky,message:Keren}]

答案 1 :(得分:0)

您可以这样做:

detailItem['item']['review'] = [
          {
             'name' : 'Adit',
             'message' : 'Cool !',
          },
          {
             'name' : 'Julian',
             'message' : 'Bagus',
          },
          {
             'name' : 'Ricky',
             'message' : 'Keren',
          }
        ]; 

然后您将在review节点中获得以下内容:

review: [{name: Adit, message: Cool !}, {name: Julian, message: Bagus}, {name: Ricky, message: Keren}]}}

答案 2 :(得分:0)

  List<Map<String, String>> abc = detailItem['item']['review'];
  print(abc.first["name"] == "Adit");


 print(detailItem['item']['review']);