How to declare async function as a variable in Dart?

Technology CommunityCategory: FlutterHow to declare async function as a variable in Dart?
VietMX Staff asked 3 years ago

Async functions are normal functions with some sugar on top. The function variable type just needs to specify that it returns a Future:

class Example {
  Future<void> Function() asyncFuncVar;
  Future<void> asyncFunc() async => print('Do async stuff...');

  Example() {
    asyncFuncVar = asyncFunc;
    asyncFuncVar().then((_) => print('Hello'));
  }
}

void main() => Example();