I want to return a list of members via a StreamController.
batches collection contains batch details and ids of members assigned to the batch.
So, in-order to get the list of members in a batch, have to loop through batch collection and get the ids of members, then match with members collection and return the matching member data as stream.
final CollectionReference _batchCollectionReference =
Firestore.instance.collection('batches');
final CollectionReference _membersCollectionReference =
Firestore.instance.collection('members');
final StreamController<List<Member>> _membersController =
StreamController<List<Member>>.broadcast();
Stream getMembers(String batchId) { //passing a batch id
_batchCollectionReference
.document(batchId)
.snapshots()
.map((batchSnapshot) => Batch.fromData( //return as Batch type
data: batchSnapshot.data, batchId: batchSnapshot.documentID))
.listen((snapshot) {
List<String> members = snapshot.members; //list of members
members.forEach((member) {
var data = _membersCollectionReference
.document(member)
.snapshots()
.map((memberData) => Member.fromData(data: memberData.data)); //return as Member type
_membersController.add(data);
});
});
return _membersController.stream;
}
}
The problem is I couldn't able to push the member data to the StreamContoller.
It says,
The argument type 'Stream<Member>' can't be assigned to the parameter type 'List<Member>'
The stream should contains instance of members; Ex: [[instance of 'Member'], [instance of 'Member'], [instance of 'Member']]
If I got the data like this way, it would be easy to loop and do the other stuff.
I couldn't able fix this issue. Any help would be appreciated.
StreamContoller(andStream.listen()) at all: just useStreamAPI likemap/asyncMAp/expand(or the most generictransform) etc