3

Sometimes you have an async task that has sub-tasks that are async in regards to each other but have a number synchronous subtasks.

For example, It's cleaning day:

Future<bool> clean(String day) async {
  bool allIsClean = false;
  if (day == 'sunday') {
    bool floorIsClean = await;  // Pick up items, vacuum flor, mop floor (in that order);
    bool dishesAreClean = await; // collect dishes, start dishwasher, empty dishwasher (in that order);

    allIsClean = floorIsClean && dishesAreClean;
  }
  return allIsClean;
}

Cleaning floor and cleaning dishes can be done async. When the dishwasher is running we can vacuum the floor etc. But cleaning the floor must be done in the exact order (pick up, vacuum, mop) and the same goes for the dishwasher.

How can I run async blocks of code inside an async block of code without having to create new async functions for each task and calling them from within the current async block?

2
  • Is there a reason you don't want to create a new function to handle each one separate? If you create two new future functions for floor and dishes then you can run them together using Future.Wait api.dartlang.org/stable/2.4.1/dart-async/Future/wait.html Commented Aug 13, 2019 at 16:51
  • Not really. It just breaks my design pattern. I want my network class to only contain http-calls, and extracting the functions to the bottom of the file would make it less obvious where the code is executed. This goes against everything I stand for, so I can easily be turned :) Commented Aug 14, 2019 at 10:21

1 Answer 1

3
Future.wait([floorIsClean(), dishesAreClean()])

https://api.flutter.dev/flutter/dart-async/Future/wait.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.