How to Populate List with Firestore stream using GetX

Viewed 5989

I'm trying to populate an RxList (GetX Flutter package) so I can show realtime data in my Listview.builder. I tried using the following code in my Getx Controller:

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);
  }
}

But I'm getting the following exception:

'Future' is not a subtype of type 'Stream<Iterable>'

1 Answers

Just use BindStream to listen to your Stream, like this:

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