[go: up one dir, main page]

0% found this document useful (0 votes)
5 views1 page

Arrow Function

Uploaded by

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

Arrow Function

Uploaded by

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

Arrow Function

Saturday, June 8, 2024 7:49 PM

Features and Use Cases:


1. Passing as Arguments
Arrow functions are frequently used as arguments to higher-order functions.
Example:
void main() {
var list = ['apple', 'banana', 'orange'];
list.forEach((item) => print(item));
}
2. Assigning to Variables
Arrow functions can be assigned to variables for reuse.
Example:
void main() {
var multiply = (int a, int b) => a * b;
print(multiply(3, 4)); // 12
}
3. Returning from Functions
Functions can return arrow functions, useful for encapsulating function behavior.
Example:
Function makeMultiplier(int multiplier) {
return (int value) => value * multiplier;
}
void main() {
var triple = makeMultiplier(3);
print(triple(5)); // 15
4. Closures
Arrow functions can capture variables from their lexical scope, creating closures.
Example:
Function makeCounter() {
int count = 0;
return () => count += 1;
}

void main() {
var counter = makeCounter();
print(counter()); // 1
print(counter()); // 2
}
Syntax of Arrow Functions
Arrow functions in Dart use the `=>` syntax and are particularly suitable for single-expression
functions.
Example:
var list = [1, 2, 3];
var squaredList = list.map((item) => item * item).toList();
print(squaredList); // [1, 4, 9]

-Arrow Functionsprovide a concise syntax for defining functions in Dart.


- They are useful for passing as arguments, assigning to variables, returning from functions, and
creating closures.
- Arrow functions are particularly suitable for single-expression functions, enhancing code
readability and conciseness.
Arrow functions are a powerful feature in Dart, enabling developers to write more expressive
and compact code, especially for functions with straightforward behavior.

Dart Page 1

You might also like