我在Firestore中有一个名为“名称”的数组。我想在ListView.builder中显示该数组元素。我尝试了很多方法,但是做不到。我不知道如何访问快照数据。
FutureBuilder(
future: getList(),
builder: (context, AsyncSnapshot<List<dynamic>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
} else {
return Center(
child: ListView.builder(
padding: const EdgeInsets.only(bottom: 20.0),
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return Center(
child: ListTile(
title: Text(
snapshot.data[0].data), //snapshot data should dispaly in this text field
),
);
}),
);
}
},
),
这是我的getList()方法。
Future<List<dynamic>> getList() async {
var firestore = Firestore.instance;
DocumentReference docRef =
firestore.collection('RecodeBook').document('2019-05-04');
List<dynamic> info = new List<String>();
docRef.get().then((datasnapshot) {
if (datasnapshot.exists) {
info = datasnapshot.data['Names'].toList();
print('#');
print(info); //this line prints [aa, aghshs, fffg, fug, ghh, fggg, ghhh]
print(info.length); //this line prints 7
}
});
return info;
}
答案 0 :(得分:0)
这似乎是因为您发回的是List<dynamic>
,而不是Future<List<dynamic>>
。以下代码应该可以工作
Future<List<dynamic>> getList() async {
var firestore = Firestore.instance;
DocumentReference docRef = firestore.collection('RecodeBook').document('2019-05-04');
return docRef.get().then((datasnapshot) {
if (datasnapshot.exists) {
List<dynamic> info = datasnapshot.data['Names'].toList();
print('#');
print(info); //this line prints [aa, aghshs, fffg, fug, ghh, fggg, ghhh]
print(info.length); //this line prints 7
return info;
}
});
}
另外,请在您的ListView.builder
中记下索引。
title: Text(
snapshot.data[index].data), //snapshot data should display in this text field