How to use Lambda functions in Dart programming

Lambda functions are a short and simple way to express tiny functions. Lambda functions are also known as anonymous functions or closures.They are functions without a name that can be used inline within the code.

return_type var_name = (parameters)=> expression;

Note: When using the Lambda function syntax, only one expression can be returned, and it must be a single-line expression.

Examples

How to use a Lambda function to add two numbers

// Using lambda function
final addNum = (int a, int b) => a + b;
main() {
print(addNum(15, 7));
}

How to use a Lambda function to display a message on the screen

void main() {
displayMsg();
}
final displayMsg = ()=>
print("This is a answer on Lambda functions in Dart programming");

How to use a Lambda function to concatenate strings

// Using lambda function
final showData = (String a, String b) => a + b;
main() {
print(showData("Educative ", "answer"));
}

Finally, if a function only returns one expression, we may use the Lambda function to represent it in only one line.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved