Blogs By Shankar
Dart
Dart
  • Dart Tutorial by Dès Vu Technologies
  • hello
  • Dart Collection
    • collectionInDart
  • File Handeling
    • fileHandelingInDart
  • Functions
    • functionParameters
    • Types of Functions in Dart
    • Functions in Dart
    • Annonymous Function
      • AnnonymousFunction
    • Arrow Function
      • arrowFunctions
    • BuiltinFunctions
      • importantBuiltinFunctions
      • mathFunction
  • OOP With Dart
    • encapsulation
    • oopWithDart
    • Generic In Dart
      • generic
    • constructor
      • constructor
      • factory constructor
        • factoryConstructor
      • initializer list constructor
        • Initializer List Constructor:
    • async dart
      • asyncAndAwait
        • asyncAndAwait
      • future
        • future
      • streams
        • streams
  • Sync and Async dart
    • syncAndAsyncDart
  • controlFlow
    • controlFlow
  • dataTypes
    • Dart Built-In Data Types
    • TypeConversion
    • String
      • stringOperations
      • string_jnterpolation
    • operators
      • Operators In Dart
  • operators
    • operators
  • user_input
    • userInput
  • variablesAndConstants
    • scopeInDart
    • variableTypesInDart
    • variables
Powered by GitBook
On this page
  1. Functions
  2. Annonymous Function

AnnonymousFunction

In Dart, anonymous functions, also known as lambda functions or closures, are functions that
don't have a name. They are often used for short, one-off operations and can be assigned to
variables or passed as arguments to other functions. Here's how you can define anonymous
functions in Dart:
(parameter1, parameter2, ...) {
  // Function body
  // Code to be executed when the function is called
}

Key points about anonymous functions:

  • Anonymous functions can take zero or more parameters.

  • They are defined using the => syntax.

  • They are often used for short, single-expression functions.

Let's explore some examples of anonymous functions in Dart:

Basic Anonymous Function:

var add = (int a, int b) => a + b;

In this example, we define an anonymous function assigned to the variable add. It takes two int parameters a and b and returns their sum.

Anonymous Function as an Argument:

Anonymous functions are commonly used as arguments in higher-order functions like forEach, map, and filter. Here's an example using the forEach method:

List<int> numbers = [1, 2, 3, 4, 5];

numbers.forEach((number) {
  print(number);
});

In this example, an anonymous function is passed as an argument to forEach. The anonymous function takes each number from the list and prints it.

Arrow Function as an Anonymous Function:

Arrow functions are often used for very concise anonymous functions. Here's an example of an arrow function as an anonymous function:

var multiplyByTwo = (int x) => x * 2;

In this case, the anonymous function takes an integer x and returns its double.

Use of Anonymous Function in Sorting:

You can use anonymous functions to define custom comparison logic for sorting. For example, sorting a list of strings by length:

List<String> names = ['Alice', 'Bob', 'Charlie', 'David'];

names.sort((a, b) => a.length - b.length);

In this example, we use an anonymous function as a comparator to sort the list of names by their lengths.

Anonymous functions are versatile and allow you to write concise, inline functions for various purposes in your Dart code. They are particularly useful when you need short, throwaway functions for specific tasks.

PreviousAnnonymous FunctionNextArrow Function

Last updated 1 year ago