Flutter:如何在Map内访问数组数据? [英] Flutter: How to access array data inside a Map?
本文介绍了Flutter:如何在Map内访问数组数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发Flutter应用,并且正在获取如下所示的JSON数据
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]}
我需要访问 members
下的每个单独元素,这是我尝试过的.
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.
但是我无法通过使用诸如 map ["members"] [0]
之类的东西来访问单个元素,因为它是一个地图.在这种情况下,我该如何访问这些数据?
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?
推荐答案
您必须将其显式转换为 List
You have to explicitly convert it to a List
尝试
List<dynamic> members = List.of(map["members"]);
或
List<dynamic> members = map["members"];
这篇关于Flutter:如何在Map内访问数组数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文