PeakGen
PeakGen

Reputation: 22995

Flutter: How to access array data inside a Map?

I am developing a Flutter app and I am getting a JSON data like below

{chat_mode: supplier, timestamp: 1605852885271, last_message: Test, members: [K9ioYyiEUQVVNjx0owwsABCD, K9ioYyiEUQVVNjx0owwsaXYZ]}

I need to access each individual element under members and this is what I tried.

Map<dynamic, dynamic> map = snapshot.value;
print(map["members"]);

The above allows me to access the entire array, not each record.

But i can't access the individual element by using something like map["members"][0] because it is a map. In this case, how can i access this data?

Upvotes: 0

Views: 601

Answers (1)

shb
shb

Reputation: 6277

You have to explicitly convert it to a List

try

List<dynamic> members = List.of(map["members"]);

or

List<dynamic> members = map["members"];

Upvotes: 1

Related Questions