'Dart: Wait all StreamSubscription async onData event functions to complete

I have this specific code:

Stream<MimeMultipart> mm =
    MimeMultipartTransformer(boundary).bind(Stream.fromIterable(l));

// Completer. So we can wait for the two response results.
Completer<dynamic> allComplete = Completer<dynamic>();
List<Future> myAsyncs = [];
myAsyncs.add(allComplete.future);
int listenCalledCount = 0;

mm.listen((event) async {
  // this listen event executes multiple times...
  Completer<dynamic> listenEventCompleted = Completer<dynamic>();
  await Future.delayed(const Duration(seconds: 10));
  print("Listening!!");
  myAsyncs.add(listenEventCompleted.future);
  listenCalledCount++; // How many times onData is called..
}, onDone: () => allComplete.complete(true));

await Future.wait(myAsyncs);
// I want all listen function called to be done first before continuing to the code below..
print(listenCalledCount);

print("All Done!");

My expected results are:

flutter: 3
flutter: All Done!

But what currently happening is:

flutter: 0
flutter: All Done!

What it means is, it doesn't wait for the StreamSubscription OnData event functions which is executed more than once to complete. I want all the OnData event functions to complete before proceeding. Is this possible?



Solution 1:[1]

  • The "error" was on my part in that the allowed domains needed several more variations, after adding
        colly.AllowedDomains(
                  "www.bjjheroes.com/", 
                  "bjjheroes.com/",
                  "https://bjjheroes.com/",
                  "www.bjjheroes.com", 
                  "bjjheroes.com",
                  "https://bjjheroes.com",
                ),

everything worked

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cade