我从NoSql数据库收到以下响应。
它的类型为InternalLinkedHashMap。
// key // value
{
"42514738" : "iohuahges",
"25498724" : "hiuasfhyd",
"24257+62" : "opiasurj",
"35842942" : "ftysadyg",
"98472442" : "iouyaerj"
}
我想使用这些值来构造一个ListView。但是以上不是列表,我将无法提前知道上面 map 中的键。
因此,即使下面的方法可以工作(下面的String赋值),我也无法在运行时执行,因为我不知道键是什么。
如何从以上结果中获取索引?
FutureBuilder(
future: methodReturningAboveMap(),
builder: (context, snapshot) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
// want to use index something like snapshot.data['collection'][0] and get get first key value and so on.
String s = snapshot.data['collection']['42514738']; // will work but can't do it as mentioned above
},
);
},
),
最佳答案
您可以提取数据并将其转换为Map
然后在ListView中,您可以使用Map<String, dynamic> data = snapshot.data['collection'];
然后使用String key = data.keys.elementAt(index);
String value = data[key];
关于flutter - 从通话中获取 map 结果列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64562911/