0% found this document useful (0 votes)
52 views2 pages

DART Arrow Function

Dart arrow functions provide a compact syntax for functions with a single line of code, allowing for more concise expressions. The syntax requires that the return type matches the value returned by the expression. Both named and anonymous functions can be converted to arrow functions to enhance code readability.

Uploaded by

nikkampallavi57
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views2 pages

DART Arrow Function

Dart arrow functions provide a compact syntax for functions with a single line of code, allowing for more concise expressions. The syntax requires that the return type matches the value returned by the expression. Both named and anonymous functions can be converted to arrow functions to enhance code readability.

Uploaded by

nikkampallavi57
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Dart Arrow Functions

Introduction to the Dart Arrow Functions


If a function body has only one line, you can use an arrow function with the
following syntax to make it more compact:

returnType functionnName(parameters) => expression;Code language: Dart (dart)

In this syntax, the return type of the function must match the type of the
value returned by the expression.

For example, the following defines the add() function that returns the sum of
two integers:

int add(int x, int y) {


return x + y;
}Code language: Dart (dart)

Since the body of the add() function has only one line, you can convert it to
an arrow function like this:

int add(int x, int y) => x + y;Code language: Dart (dart)

Also, if an anonymous function has one line, you can convert it to an arrow
function to make it more compact:

(parameters) => expression;Code language: Dart (dart)

For example, the following defines an anonymous function that returns the
sum of two integers and assigns it to the add variable:

void main() {
var add = (int x, int y) {
return x + y;
};

print(add(10, 20));
}Code language: Dart (dart)

Since the anonymous function has one line, you can use the arrow function
syntax like this:

void main() {
var add = (int x, int y) => x + y;
print(add(10, 20));
}Code language: Dart (dart)

Summary
 Use arrow functions for the functions with one line to make the code
more concise.

You might also like