Home > OS >  How to use async/await in Dart for parallel processing
How to use async/await in Dart for parallel processing

Time:06-08

In C# I can use async and await to process tasks in parallel. I can kick off an asynchronous task, do other things, and finally await for the asynchronous task to complete.

var t = myFunctionAsync();
executeTask1();
executeTask2();
await t;

How can I do this in dart/flutter?

CodePudding user response:

Reference: async-await

void main() async {
  var t = myFunctionAsync();
  executeTask1();
  executeTask2();
  await t;
}

Future<int> myFunctionAsync() async {
  await Future.delayed(const Duration(seconds: 2));
  return 2;
}

void executeTask1() {
  // do something
}

void executeTask2() {
  // do something
}

CodePudding user response:

void someFunction () async{
  executeTask1();
  executeTask2();
  await executeTask3();
}
  • Related