Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

dart

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

What does this syntax (=>) mean?

_MyAppState createState() => _MyAppState();
like image 918
Sreejith N S Avatar asked Dec 17 '20 15:12

Sreejith N S


People also ask

What is Arrow function in DART?

Dart arrow function The arrow function allows us to create a simplyfied function consisting of a single expression. We can omit the curly brackets and the return keyword.

What is lambda in Dart?

Lambda functions are a short and simple way to express tiny functions. Lambda functions are also known as arrow functions. Like any other function, a Lambda function cannot execute a block of code.

Which is the correct form of shorthand function in DART?

The => e syntax is shorthand for a function body of the form {return e;}. Notice how the return type is omitted. Dart is an optionally typed language, so the programmer is free to omit static type declarations.


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;
like image 58
MendelG Avatar answered Oct 19 '22 15:10

MendelG