如何使用GetX使用Firestore流填充列表

时间:2020-08-03 16:42:50

标签: flutter google-cloud-firestore

我正在尝试填充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 '类型的子类型

1 个答案:

答案 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());
}