Higher Order Functions in Dart & Flutter:

Higher Order Functions in Dart & Flutter:

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:

  • Anonymous function are the function without a name, we can write multiple lines in body of this. We return the value explicitly in this function using return keyword.

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

  • Lambada Expression are also a function without name, but we can return a code of single line expression using lambda expression. We don not to return the value explicitly in Lambda Expression. We us arrow expression(=>) to return the expression from this types of function.

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.


要查看或添加评论,请登录

Arpan Bhatia的更多文章

  • Closures in Flutter

    Closures in Flutter

    A Closure is Function in Flutter that capture a value from its surroundings scope, even after scope exited. It is…

  • Extensions in Flutter

    Extensions in Flutter

    In Flutter extensions allowed to you to add some new functionality to existing class without modified their original…

  • Mixin in Dart:

    Mixin in Dart:

    As the name suggest Mixin is use to mix it with some thing .In Flutter Mixin is way to reuse a code into multiple class…

    1 条评论
  • Kotlin Flows and Channels

    Kotlin Flows and Channels

    In modern software development, asynchronous programming has become a crucial aspect of building responsive and…

    1 条评论
  • StateLess Widget in Flutter

    StateLess Widget in Flutter

    Stateless widget simply means the widget that will be return in this class will not contain any state and it will never…

社区洞察

其他会员也浏览了