[英]Listview.builder from firebase
I have this code here which supposedly builds a list from a document in firebase, which ultimately fails as it always goes to return loading
.我这里有这段代码,它应该从 firebase 中的文档构建一个列表,最终失败,因为它总是
return loading
。 From what I know, it has something to do with it being a future and I think I am accessing it wrongly.据我所知,它与未来有关,我认为我错误地访问了它。 I have tried getting the output as Text and it works, but as aa listview, it does not.
我已经尝试将 output 作为文本获取并且它有效,但作为 aa 列表视图,它没有。
I also tried making a function with async on it but the app still outputs loading.我还尝试制作一个带有异步功能的 function,但该应用程序仍会输出加载。 Any help would be appreciated.
任何帮助,将不胜感激。
Widget showFriend() {
CollectionReference users = FirebaseFirestore.instance.collection('todos');
return FutureBuilder<DocumentSnapshot>(
future: users.doc(documentId).get(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.hasData && !snapshot.data!.exists) {
return Text("Document does not exist");
}
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data =
snapshot.data!.data() as Map<String, dynamic>;
List<dynamic> fren = [];
void waitList() async {
List<dynamic> temp;
temp = await (data['friends']);
fren = temp;
}
waitList();
fren = List.from(data['friends']);
print(fren);
if (fren.length > 0) {
ListView.builder(
itemCount: fren.length,
itemBuilder: (context, index) {
return ListTile(title: Text('${fren[index]}'));
});
}
}
return Text("loading");
});
}
You are missing return
before ListView
您在
ListView
之前缺少return
if (fren.length > 0) {
return ListView.builder( //here
try the following:尝试以下操作:
Widget showFriend() {
CollectionReference users =
FirebaseFirestore.instance.collection('todos');
// ignore: newline-before-return
return FutureBuilder<DocumentSnapshot>(
// future: users.doc(documentId).get(),
// ignore: prefer-trailing-comma
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
if (!snapshot.data!.exists) {
return Text("Document does not exist");
} else {
Map<String, dynamic> data =
snapshot.data!.data() as Map<String, dynamic>;
List<dynamic> fren = [];
List<dynamic> temp;
temp = data['friends'];
fren = temp;
fren = List.from(data['friends']);
print(fren);
if (fren.length > 0) {
ListView.builder(
itemCount: fren.length,
itemBuilder: (context, index) {
return ListTile(title: Text('${fren[index]}'));
});
}
}
}
}
return Text("loading");
},
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.