Higher Order Functions in Dart & Flutter:
Arpan Bhatia
8+ Years Experience, I am looking for New Opportunities, Contact at: 7906156955 || Sr. Developer @Team Computers Mobile Team || Android || Core Java || Kotlin || Flutter || Dart || Jetpack Components ||Jetpack Compose UI
Higher Functions are the function that are used to make our code more concise, flexible and reusable. A higher order function is a function that are used to pass a function as Argument in another function and to return a function from another function as a result.
We use Function keyword as a data type of an argument or as return type in a these Function
Anonymous Function and Lambda Expression in Dart:
void main(){
var add = (int a, int b) //Start of Anonymous Function
{
int sum = a+b;
return sum;
} //End of Anonymous Function
print(add(2,3)); }
Output : 5 // print() function Accept a Value of Object type so we can pass Function here
void main(){
var add = (int a, int b) => a+b; //Lambda Expression
print(add(3,2));
}
Output : 5
Let we check more details about Higher Order Function in detail with code for more clarity about this.
Example 1-
Function that returns a Function:
Function(int) createAdder(int addBy){
return (int i) => i+addBy; //lambda expression used here
}
Now we will use this function in main() methos to pass this as an argument:
void main(){
var add2 = careteAdder(3)
print(add2(3))
}
Output : 5
Example 2-
Function Taking another Function as an Argument:
void applyFunctiontoValue(Function(int) func, int value)
{
print(func(value))
}
void main()
{
Function(int) addTwo = createAdder(2);
applyFunctiontoValue(addTwo, 5);
}
Output : 7
Example 3:
Passing Function as an Argument:
领英推荐
void printElement(int Element)
{
print(element) //print has Argument of 'Object' type so we can pass any data type in it
}
void main()
{
var list = [1,2,3]
list.forEach(printelement)
}
Output :
1
2
3
Note: forEach() function excepts a Function in the Argument and work according to provided callback function, and internally calls this callback once for every element in the collection.
Example 4:
forEach using Anonymous Function:
void main()
{
var list2 = ['apple','banana','orange'];
list2.forEach
(
(item)
{
print(item);
}
)
forEach using Lambada Expression:
void main()
{
var list2 = ['apple','banana','orange'];
list2.forEach(
(item)=>
print(item)
);
}
Output:
apple
banana
orange
Note:
Flutter uses abstraction extensively through higher-order functions. By using higher-order functions, Flutter allows developers to focus on the "what" rather than the "how" in many scenarios. Scenarios where Flutter achieves abstraction with higher-order functions:
1. Widget Builders
2. Gesture Detection and Event Handling
3. State Management (Functional State Logic)
4. Functional Programming for Collection Transformations
5. Animation and Interpolation
This approach keeps the framework flexible and powerful while letting developers focus on the business logic and UI design without worrying about underlying complexities.
Conclusion:
Higher-order functions make code more expressive, reusable, and maintainable, forming a cornerstone of programming in Dart and Flutter. Their widespread use in Flutter simplifies UI building and event-driven programming, empowering developers to create sophisticated applications efficiently.