Component - Functions and arrow syntax
This Flutter component shows how to use functions with arrow syntax to handle a button press. The arrow syntax is a short way to write functions that return a single expression.
This Flutter component shows how to use functions with arrow syntax to handle a button press. The arrow syntax is a short way to write functions that return a single expression.
Scaffold
├─ AppBar
│ └─ Text
└─ Center
└─ Column
├─ Text
└─ ElevatedButton
└─ Text=> in a Flutter function do?=> is a shortcut for functions that return a single expression.a and b?=> followed by an expression without braces or return keyword.a + b. The other options misuse braces or return keyword.int multiply(int x, int y) => x * y;
void main() {
print(multiply(3, 4));
}x and y using arrow syntax.int add(int a, int b) => {
return a + b;
};s.isNotEmpty. int lengthOrZero(String s) => if (s.isNotEmpty) s.length else 0; uses invalid if-else syntax. int lengthOrZero(String s) => { return s.length; }; uses braces and return which is invalid for arrow. int lengthOrZero(String s) => s.length > 0; returns a boolean instead of int.Pass the test and this pattern moves unknown