What is Fat Arrow Notation in Dart and when do you use it?

Technology CommunityCategory: FlutterWhat is Fat Arrow Notation in Dart and when do you use it?
VietMX Staff asked 3 years ago

The fat arrow syntax is simply a short hand for returning an expression and is similar to (){ return expression; }.

The fat arrow is for returning a single line, braces are for returning a code block.

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

// Normal function
void function1(int a) {
  if (a == 3) {
    print('arg was 3');
  } else {
    print('arg was not 3');
  }
}

// Arrow Function
void function2(int a) => print('arg was ${a == 3 ? '' : 'not '}3');