Search code examples
dartflutterreactive-programmingrxdart

how to use Firebase snapshot with rxdart streams


I've a class that has all firebase related functions, and another class to manage state (bloc). How to make them work consistently?.

class UserFirebase {

Stream<List<User>> fetchUsers() {
  // I want this function to return a Stream<List<User>> where i can 
  //listen to in UserBloc
  return Firestore.instance.collection('users').snapshots();
 }
}



class UserBloc {
   UserFirebase _sService;
   Observable<List<User>> get users => _sService.fetchUsers();
}

my approach might be not correct but i wanted to describe the problem.


Solution

  • You can watch this tutorial and just replace the source of the data with firebase: https://www.youtube.com/watch?v=fahC3ky_zW0

    It is what I have done an it works for me. The difference with the tutorial is that, I stream a "List" while the stream "UnmodifiableListView". Before you stream, you have to map your data to a list. You can then use the bloc everywhere you want.