Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why async keyword function without await keyword in Dart?

Tags:

flutter

dart

I saw many people's code use async keyword in a function without an await keyword in the function body. Even some official flutter example code do this. I have no idea why. What is the point? Is this a mistake or having a purpose?

Normally, I just remove the async keyword from those code and everything will run without any problems. Can some dart expert clarify that if there is a purpose for a function which has the async keyword but NO await keyword? Or is this just their mistake?

like image 830
sgon00 Avatar asked May 24 '19 15:05

sgon00


1 Answers

async is sometimes used to simplify code.

Here are some examples:

Future<int> f1() async => 1;
Future<int> f1() => Future.value(1);

Future<void> f2() async {
  throw Error();
}
Future<void> f2() {
  return Future.error(Error());
}
like image 169
Alexandre Ardhuin Avatar answered Nov 15 '22 07:11

Alexandre Ardhuin