Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart Future.wait for multiple futures and get back results of different types

I'm using Flutter to download 3 different sets of data from a server, then do something with all 3 sets. I could do this:

List<Foo> foos = await downloader.getFoos();
List<Bar> bars = await downloader.getBars();
List<FooBars> foobars = await downloader.getFooBars();

processData(foos, bars, foobars);

But I'd prefer to download all 3 data sets asynchronously in parallel. I've seen that Dart has this Future.wait method:

Future<List<T>> wait <T>(
   Iterable<Future<T>> futures, {
   bool eagerError: false, 
   void cleanUp(
      T successValue
   )
}) 

However it looks like this will only return values of the same type (T). I have 3 different types, so I don't see how I can use this and get my 3 data sets back.

What's the best alternative way to achieve this?

Thanks!

like image 884
James Allen Avatar asked Apr 08 '20 16:04

James Allen


People also ask

What are async await and futures in Dart?

Summary of asynchronous programming in DartAsynchronous function is a function that returns the type of Future. We put await in front of an asynchronous function to make the subsequence lines waiting for that future's result. We put async before the function body to mark that the function support await .

How do futures work in Dart?

A Future<T> instance produces a value of type T . If a future doesn't produce a usable value, then the future's type is Future<void> . A future can be in one of two states: uncompleted or completed. When you call a function that returns a future, the function queues up work to be done and returns an uncompleted future.

How do you get rid of futures in darts?

What you can do to resolve your future is, add a . then() after the function call so it waits for the future and when it comes, converts it to the data type that you want.

How do you create a Future variable in darts?

To create a Future object, we need to instantiate Future class by passing an invoker function (usually an anonymous function or fat arrow function) whose job is to return a value or another Future object that returns a value. A Future object has then and catchError methods which are used to register callback functions.


1 Answers

You need to adapt each of your Future<T>s to a common type of Future. You could use Future<void>:

List<Foo> foos;
List<Bar> bars;
List<FooBars> foobars;

await Future.wait<void>([
  downloader.getFoos().then((result) => foos = result),
  downloader.getBars().then((result) => bars = result),
  downloader.getFooBars().then((result) => foobars = result),
]);

processData(foos, bars, foobars);

Or if you prefer await to .then(), the Future.wait call could be:

await Future.wait<void>([
  (() async => foos = await downloader.getFoos())(),
  (() async => bars = await downloader.getBars())(),
  (() async => foobars = await downloader.getFooBars())(),
]);
like image 76
jamesdlin Avatar answered Oct 10 '22 23:10

jamesdlin