makurisan
makurisan

Reputation: 529

Accessing a list inside a map

I want to access the 'addr' field of the following map and tried:

var v1 = mapd['addr'][0] 

but without success. What is wrong with it?

also

String str = mapd['addr'][0].toString();

an exception appears.

enter image description here

@EDIT

It is a list like this

  var myMapList ={
    'key3': 'sssss',
    'key1':[9,0,0],
    'key2':[7,0,0],
  };

Upvotes: 0

Views: 400

Answers (3)

makurisan
makurisan

Reputation: 529

I have not all mentioned in the above question. I create the Map in a c program and the format is "msgpack". The map is then sent to flutter via Bluetooth SPP. The flutter app deserialze the map and then I called:

var v1 = mapd['addr'][0] 

The problem was not inside flutter. It was a wrong created msgpack map and the deserialize from msgpack2 have not mentioned that the format is wrong and this resulted in a wrong flutter map.

I have checked the creation of the field in the C program and fixed it. The array field count was wrong.

Upvotes: 0

Murilo Erhardt
Murilo Erhardt

Reputation: 48

Following your sample, you should try that:

print((myMapList["key1"] as List)[0]);

Run all code in dartpad or similar to check:

void main() {
  var myMapList = {
    'key3': 'sssss',
    'key1': [9, 0, 0],
    'key2': [7, 0, 0],
  };

  print((myMapList["key1"] as List)[0]);

  var listFromMap = myMapList["key1"] as List;

  listFromMap.forEach((value) => {print(value)});
}

Upvotes: 1

MoAshour
MoAshour

Reputation: 179

According to the above image you can try access it using the below:

var v1 = mapd[1]; // This will get you the second element which represents your 
                  // addr

Upvotes: 0

Related Questions