Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

`extension FutureIterable<T>.wait` does not work properly in Dart/Flutter

extension FutureIterable<T>.wait does not work properly

from the documentation:

/// Similar to [Future.wait], but reports errors using a
/// [ParallelWaitError], which allows the caller to
/// handle errors and dispose successful results if necessary.

The following code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Future<int> main() async {
  await process([1, 2, 3, 4, 5]);
  return 0;
}

Future<void> process(Iterable<int> messages) async {
  print("len: ${messages.length}");
  Iterable<Future<int>> futures = messages.map((message) {
    return fibo(message);
  });
  assert(messages.length == futures.length);
  try {
    print("before big wait");
    await futures.wait;
    print("after big wait");
  } catch (e) {
    print(e.runtimeType);
    print("$e from the end");
  }
  print("stop!");
}

Future<int> fibo(int message) {
  throw "Test error handling";
}

output:

flutter: len: 5
flutter: before big wait
flutter: String
flutter: Test error handling from the end
flutter: stop!

I don’t see any sign of ParallelWaitError, as the documentation mentions. And the future returns an error at the first exception.

What do I miss?

>Solution :

Instead of using throw, try using Future.error to return an error from your function:

Future<int> fibo(int message) {
  return Future.error("Test error handling");
}

The result will show the ParallelWaitError as you expect:

len: 5
before big wait
ParallelWaitError<List<int?>, List<AsyncError?>>
ParallelWaitError(5 errors): Test error handling from the end
stop!
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading