Frank Saputra
Frank Saputra

Reputation: 18

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

Initially, I declared a variable named detailItem with the data type 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

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:

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 ??

I just want to fill in the JSON data that I got from the API and then put it in a variable.

Upvotes: 0

Views: 460

Answers (3)

ibrahimkarahan
ibrahimkarahan

Reputation: 3015

Update: Also u can do it 1 line

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}]

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}]

Upvotes: 1

Hardik Kumbhani
Hardik Kumbhani

Reputation: 2021

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


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

Upvotes: 0

Peter Haddad
Peter Haddad

Reputation: 80914

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: [{name: Adit, message: Cool !}, {name: Julian, message: Bagus}, {name: Ricky, message: Keren}]}}

Upvotes: 0

Related Questions