2

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.

David L
  • 1,134
  • 7
  • 35
  • https://stackoverflow.com/questions/36571924/how-can-i-merge-multiple-streams-into-a-higher-level-stream, or https://stackoverflow.com/questions/54169982/how-we-can-mix-streams-in-dart? – Frank van Puffelen Sep 23 '21 at 01:27
  • Thanks. I was testing, but I couldn't get the solution – David L Sep 23 '21 at 02:05
  • You may try to use .snapshots().asyncMap((event) async { load second part and append to the total list } But the second is not a stream. For me this was good enough. In your case the stream should be on the pending tasks to get the realtime updates. – Felix Mar 18 '23 at 15:15

0 Answers0