What's the meaning of “=>” (arrow) in Dart/Flutter?

Viewed 17211
  [
    Provider<FirebaseAuthService>(
      create: (_) => FirebaseAuthService(),
    ),
    Provider<ImagePickerService>(
      create: (_) => ImagePickerService(),
    ),
  ],

What does this syntax (=>) mean?

_MyAppState createState() => _MyAppState();
1 Answers

From the documentation:

For functions that contain just one expression, you can use a shorthand syntax. The => expr syntax is a shorthand for { return expr; }. The => notation is sometimes referred to as arrow syntax.

Note: Only an expression—not a statement—can appear between the arrow (=>) and the semicolon (;). For example, you can’t put an if statement there, but you can use a conditional expression.


Code example:

The following function:

int sum(int x, int y) {
  return x + y;
}

Is the same as:

int sum(int x, int y) => x + y;
Related