I am trying to make my Flutter app with chat.
I use a streambuilder to update the chat data.
My problem is i don't know how i can read multiple documents with streambuilder.
My plan i would like get all data from this documents.
Before, i storage the id in a object and i use a for loop to get all data from documents.
The documents can be a random count (2 or 10...).
Here my streambuilder:
body() {
//build stream link get id
Stream _build_stream_id() async* {
//load user
var user_id = await StorageUserID.loadData();
yield* FirebaseFirestore.instance
.collection('chat')
.doc('users')
.collection(user_id)
.snapshots();
}
//build stream link get data
Stream _build_stream_data(chat_overview_object, index) async* {
yield* FirebaseFirestore.instance
.collection('chat')
.doc('chat_overview_data')
.collection('data')
.doc(chat_overview_object[index].chat_overview_id[0])
.snapshots();
}
return StreamBuilder(
stream: _build_stream_id(),
builder: (context, snapshot) {
if (snapshot.hasData) {
var chat_overview_object = query_chat_overview_data_1(snapshot.data);
for (var i = 0; i < chat_overview_object.length; i++) {
return StreamBuilder(
stream: _build_stream_data(chat_overview_object, i),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text('test: ' + snapshot.data.toString());
} else {
return Text("No data");
}
},
);
}
} else {
return Text("No data");
}
return Text('data');
},
);
}
If you find a better way pls tell me.
If you have questions, feel free to ask me.
Many thx (:
You can combine streams using the StreamGroup from the async package.
import 'package:async/async.dart' show StreamGroup;
List<Stream> streamList = [stream1, stream2];
Stream combinedStream = StreamGroup.merge(streamList);
Though handling Firestore sollections with different fields might be tricky.