Elvis Chen
Elvis Chen

Reputation: 13

How to convert a LIst<> to Stream<List<>> in dart for flutter streambuilder

I accessed value from a field in FireStore but it's in a form of a list and I need to transform it to a Stream<List<>> to put in a streambuilder. How would I do that? Thanks in advance

Upvotes: 1

Views: 1813

Answers (1)

Robert Sandberg
Robert Sandberg

Reputation: 8635

Instead of getting the value using get() on a document reference you can use:

Stream documentStream = FirebaseFirestore.instance.collection('myCollection').doc('ABC123').snapshots();

You can use snapshots directly with your StreamBuilder:

final Stream<QuerySnapshot> _usersStream = FirebaseFirestore.instance.collection('users').snapshots();

Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: _usersStream,
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          ....
}

Upvotes: 1

Related Questions