Flutter: How to access array data inside a Map?

时光总嘲笑我的痴心妄想 提交于 2021-02-11 15:17:47

问题


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?


回答1:


You have to explicitly convert it to a List

try

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

or

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


来源:https://stackoverflow.com/questions/64927867/flutter-how-to-access-array-data-inside-a-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!