简体   繁体   English

为什么我不能将元素添加到 map 数据类型(Dart/Flutter)

[英]Why can't I add elements to map data type (Dart/Flutter)

Initially, I declared a variable named detailItem with the data type Map .最初,我声明了一个名为detailItem的变量,其数据类型为Map After that, I fill the variable with a variety of data, but when changing it occurs an error.之后,我用各种数据填充变量,但是在更改它时会发生错误。 For more details, see the code that I included below.有关更多详细信息,请参阅我在下面包含的代码。

I've tried searching google and documentation, but I still haven't gotten the answer.我已经尝试搜索谷歌和文档,但我仍然没有得到答案。

dart dart

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';

I hope the contents of the variable from detailItem are like this:我希望detailItem中的变量内容是这样的:

dart dart

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',
          }
        ]
      }
    };

But I get an error in the code:但我在代码中得到一个错误:

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

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

Can a variable with a map data type not be filled with: x [integer] = value ??具有 map 数据类型的变量是否可以不填充: x [integer] = value ??

I just want to fill in the JSON data that I got from the API and then put it in a variable.我只想填写从 API 得到的 JSON 数据,然后放入变量中。

Update: Also u can do it 1 line更新:你也可以做到 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']);

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

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

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

You can just do:你可以这样做:

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

And then you will get in the review node the following:然后您将在review节点中获得以下内容:

review: [{name: Adit, message: Cool !}, {name: Julian, message: Bagus}, {name: Ricky, message: Keren}]}}
  List<Map<String, String>> abc = detailItem['item']['review'];
  print(abc.first["name"] == "Adit");


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM