How to access multiple streams inside a StreamBuilder?
Asked Answered
T

2

7

I've tried to make a new StreamBuilder inside the builder of my initial stream, and while I don't get any errors, it also doesn't seem like it works. Is this possible to do?

Basically, I'm accessing a root Collection in Firebase. This collection contains two documents, and I need data from each of them, as I am building a DataTable that contains information from both paths.

EDIT: The nested StreamBuilder doesn't work because StreamBuilder must return a widget, so at least I know why it wasn't working. But I still don't have a resolution on what to do.

Tocopherol answered 25/6, 2018 at 23:49 Comment(2)
You can combine streams in various ways. The rxdart package provides a large set of stream transformers for that purpose, the async from the Dart team provides a few as well.Calculate
You could combine using StreamZip or StreamGroup and with the combined stream you use .asBroadcastStream()Heigho
P
1

you can use it like this:

    StreamBuilder(
        stream: firestore.collection("nameofcollection").doc.snapshots(),
        buider: (context, AsyncSnapshot snapshot){
            if(snapshot.hasError) return Text('your error');
                if(snapshot.hasData){  
                    typeList List nameList = [];
                    snapshot.forEach((e){
                        typeObject object = ();
                        object.property1 = e.get('property1Database');
                        object.property2 = e.get('property2Database');
                        nameList.add(object);
                    });
                    //now use the list that will be loaded with the 2 documents
                 }
            }
    );
Penza answered 4/11, 2021 at 16:37 Comment(1)
in line snapshot.forEach((e){ , read snapshot.doc.forEach((e){Penza
D
0

Have you tried using StreamZip? This should help you manage multiple streams. Here's a sample on how it could be used.

import 'package:async/async.dart';

Stream<DocumentSnapshot> stream1 = firestore.document('/path1').snapshots();
Stream<DocumentSnapshot> stream2 = firestore.document('/path2').snapshots();
StreamZip bothStreams = StreamZip([stream1, stream2]);

// To use stream
bothStreams.listen((snaps) {
 DocumentSnapshot snapshot1 = snaps[0];
 DocumentSnapshot snapshot2 = snaps[1];

 // ...
});

Do you have a minimal repro of what you're trying to implement along with the steps to replicate the behavior? I also need how the data you're trying to access looks like. This will help me visualize how you're trying to run multiple streams for Firestore (I assumed since Collection was mentioned).

Dendro answered 30/9, 2020 at 17:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.