Flutter Async Load Multiple Functions One After The Other Flutter Async Load Multiple Functions One After The Other dart dart

Flutter Async Load Multiple Functions One After The Other


It seems like the load() function itself completes before the loadFeed() Future is finished, how can I fix that?

That's exactly what's wrong. You do:

load(int categoryIndex, String _url) async {  loadFeed(_url).then((result) {    // ... bunch of code...  });}

Your load function calls loadFeed, registers a Future.then callback, ignores the newly returned Future, and synchronously returns to the caller after callback registration. It does not use await to wait for the Future to complete, nor does it return the Future to the caller to allow the caller to wait for the Future.

It's much simpler if you use async/await instead of mixing it with the raw Future API:

Future<void> load(int categoryIndex, String _url) async {  var result = await loadFeed(_url);  // ... bunch of code...}

Additionally, enabling the unawaited_futures lint in your analysis_options.yaml file would have caught this problem during static analysis.


In future functions, you can use whencomplete, from the documentation:

"WhenComplete", Registers a function to be called when this future completes.

The [action] function is called when this future completes, whether itdoes so with a value or with an error.

This is the asynchronous equivalent of a "finally" block.

  • Change load into a future function, and add a return value to let dart know that you are done, like this:

    Future load(int categoryIndex, String _url) async {   loadFeed(_url).then((result) {     if (null == result || result.toString().isEmpty) {       print("error");       return;     }     data.value = [[],[],[],];     for (var i = 0; i < result.items.length; i++) {       data.value[categoryIndex].add(result.items[i]);     }    return data.notifyListeners(); //add a return here, to tell dart that this function is done.   }); }
  • Change your loadAll into this:

    loadAll() async { await load(0, 'url1').whenComplete(() async => await load(1, 'url2').whenComplete(() async => await load(2, 'url3')));}