我正在尝试填充RxList(GetX Flutter package) 这样我就可以在Listview.builder中显示实时数据。我尝试在Getx控制器中使用以下代码:
final videos = List < Video > ().obs;
@override
void onReady() async {
videos.bindStream(loadVideos());
super.onInit();
}
Stream <List<Video>> lista() {
Stream <QuerySnapshot> stream =
Firestore.instance.collection('videos').snapshots();
return stream.map((qShot) => qShot.documents
.map((doc) => Video(
title: doc.data['title'],
url: doc.data['url'],
datum: doc.data['datum']))
.toList());
}
loadVideos() async {
await
for (List < Video > tasks in lista()) {
videos.addAll(tasks);
}
}
但是我遇到了以下异常:
'Future'不是'Stream
答案 0 :(得分:2)
只需使用BindStream来收听您的Stream,就像这样:
final videos = List<Video>().obs;
@override
void onInit() async {
videos.bindStream(lista());
super.onInit();
}
Stream <List<Video>> lista() {
Stream <QuerySnapshot> stream =
Firestore.instance.collection('videos').snapshots();
return stream.map((qShot) => qShot.documents
.map((doc) => Video(
title: doc.data['title'],
url: doc.data['url'],
datum: doc.data['datum']))
.toList());
}