I am using Streambuilder to display a list of tasks. For this I want to join two queries in Firebase: The tasks that have been completed in the last 7 days (alList1
) and all the pending tasks (alList2
)
The way I do the query is as follows:
Stream<List<AlarmaModel>> cargarAlarmasStream(int fechaMinima) {
final List<AlarmaModel> alarmaListVacia = [];
Query resp = db.child('alarmas').orderByChild('fechaCompleta').startAt(fechaMinima);
final streamPublish1 = resp.onValue.map((event) {
if(event.snapshot.value != null) {
final alList1 = Map<String,dynamic>.from(event.snapshot.value).entries.map((e) {
AlarmaModel alTemp = new AlarmaModel();
alTemp = AlarmaModel.fromJson(Map<String,dynamic>.from(e.value));
alTemp.idAlarma = e.key;
return alTemp;
}).toList();
return alList1;
} else return alarmaListVacia;
});
Query resp2 = db.child('alarmas').orderByChild('completa').equalTo(false);
final streamPublish2 = resp2.onValue.map((event) {
if(event.snapshot.value != null) {
final alList2 = Map<String,dynamic>.from(event.snapshot.value).entries.map((e) {
AlarmaModel alTemp2 = new AlarmaModel();
alTemp2 = AlarmaModel.fromJson(Map<String,dynamic>.from(e.value));
alTemp2.idAlarma = e.key;
return alTemp2;
}).toList();
return alList2;
} else return alarmaListVacia;
});
return streamPublish1;
}
However, at the time of displaying it on the screen with the StreamBuilder, it is only showing me alList1
, which is the one contained in streamPublish1
. How can I do in the return to output the result of StreamPublish1
added to StreamPublish2
?
UPDATE
I was testing the solution in Stackoverflow 36571924 as follows:
Stream<List<AlarmaModel>> pubStream = StreamGroup.merge([streamPublish1, streamPublish2]);
return pubStream
It was close, but it didn't work. It only retrieves the last list (alList2
) and not the combination of both.