Table of contents
Shorthand Syntax Expression
Generally, shorthand syntax expression are concise way of writing certain code patterns that are common and repetitive.
// Longhand
int x = 10;
String result;
if (x > 5) {
result = 'x is greater than 5';
} else {
result = 'x is not greater than 5';
}
// Shorthand
int x = 10;
String result = x > 5 ? 'x is greater than 5' : 'x is not greater than 5';
Fat Arrow Notation
Generally in dart, we have fat arrow notation =>
. A fat arrow is used to define a single expression in a function.
This is a cleaner way to write functions with a single statement.
Syntax :
ReturnType FunctionName(Parameters...) => Expression;
ReturnType consists of datatypes like void, int, bool, etc..
FunctionName defines the name of the function.
Parameters are the list of parameters function requires.
Using of Arrow Function
The function
peramOfRectangle
is defined with two parameters:length
andbreadth
, representing the length and breadth of a rectangle, respectively.Instead of using curly braces
{}
to enclose a block of code for the function body, the=>
is used to directly indicate what the function returns or does. In this case, it prints out the calculated perimeter of the rectangle.The
print
statement inside the function calculates the perimeter using the formula2 * (length + breadth)
, and it interpolates this value into the string to display the result.