How to start async tasks within async tasks in Flutter?

1,685
Future.wait([floorIsClean(), dishesAreClean()])

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

Share:
1,685
Joel Broström
Author by

Joel Broström

Updated on December 13, 2022

Comments

  • Joel Broström
    Joel Broström over 1 year

    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?