Flutter firestore show data in streambuilder by function

Viewed 19

I have function like this. I need to get data from where query and need to use or but I have search there is no OR function so I have done like this.

List<Stream<QuerySnapshot>> getStreams() {
  List<Stream<QuerySnapshot>> streams = [];

  var firstQuery = FirebaseFirestore.instance
      .collection(ROOM_COLLECTION)
      .where('userId1', isEqualTo: globalProviderState.getID)
      .snapshots();

  var secondQuery = FirebaseFirestore.instance
      .collection(ROOM_COLLECTION)
      .where('userId2', isEqualTo: globalProviderState.getID)
      .snapshots();
  streams.add(firstQuery);
  streams.add(secondQuery);
  return streams;
}

And in StreamBuilder I am showing like this

      StreamBuilder<QuerySnapshot>(
            stream: getStreams(),
            builder: (BuildContext context,
                AsyncSnapshot<QuerySnapshot> snapshot) {
              if (!snapshot.hasData) {
                return new Text("Loading");
              }
            })

But its showing error that The argument type 'List<Stream<QuerySnapshot<Object?>>>' can't be assigned to the parameter type 'Stream<QuerySnapshot<Object?

I need to fix this error to get data in StreamBuilder

1 Answers

You can use multi_stream_builder package for that

Widget build(BuildContext context) {
   return MultiStreamBuilder(
   streams: [stream1, stream2],
   builder: (context, dataList) {
     final stream1Value = dataList[0];
     final stream2Value = dataList[1];
     return Text('$stream1Value, $stream2Value');
  },
 );
}

Or you can use a nested stream builder

StreamBuilder<QuerySnapshot>(
        stream: streams[0],
        builder: (BuildContext context,
            AsyncSnapshot<QuerySnapshot> snapshot1) {
          if (!snapshot.hasData) {
            return StreamBuilder<QuerySnapshot>(
        stream: streams[1],
        builder: (BuildContext context,
            AsyncSnapshot<QuerySnapshot> snapshot2) {
          if (!snapshot.hasData) {
            return new Text("Loading");
          }
        })
      }
    })
Related