In Flutter/Dart the examples sometimes show fat arrow and sometimes dont. Here are examples:
RaisedButton(
onPressed: () {
setState(() {
_myTxt = \"
=> Short hand expression is used to define a single expression in a function. Enough with the definition and properties.
Below Both are the same function and will return the same value. Just a different syntax
This is with => type
var size = (int s) => s*2;
This is return
type
var size = (int s) {
return s*2;
}
To understand the concept with a real code example. We will consider the same examples that are done in Dart functions tutorial. The code executes the Perimeter and Area of a rectangle. This is how usually it is done with the help of functions.
void main() {
findPerimeter(9, 6);
var rectArea = findArea(10, 6);
print('The area is $rectArea');
}
void findPerimeter(int length, int breadth) {
var perimeter = 2 * (length * breadth);
print('The perimeter is $perimeter');
}
int findArea(int length, int breadth) {
return length * breadth;
}
The given functions can be optimized with the help of fat arrow in Dart.
void main() {
findPerimeter(9, 6);
var rectArea = findArea(10, 6);
print('The area is $rectArea');
}
void findPerimeter(int length, int breadth) =>
print('The perimeter is ${2 * (length * breadth)}');
int findArea(int length, int breadth) =>
length * breadth;
After hitting the run button we are still getting the same result.
The perimeter is 108
The area is 60
Is from : https://flutterrdart.com/dart-fat-arrow-or-short-hand-syntax-tutorial/