What is Closure in Dart Programming Language?

In Dart Programming Language, we can use a function as a value that can be passed as a parameter to another function, or used as a return value. It can also assigned to a variable. This type of function is called a closure, although it has no difference from normal function.

In this article, we will see how closure works and how we can use typedef to make it simpler. To demonstrate, let's create two simple functions like so:

void exampleFunction(void Function(String value) helloPrinter) {
  helloPrinter("Hello, world");
}

void examplePrinter(String value) {
  print(value);
}

exampleFunction(examplePrinter);

As you can see, we define two functions, the exampleFunction and examplePrinter. Next, we call the exampleFunction and use the examplePrinter as the parameter.

Within the exampleFunction, we call the closure (the function that passed as a parameter) as a callback function.

Make it more readable with typedef

Typedef (or a type alias) makes the code more readable when working with closure. It defines the function signature that we can use as a parameter definition. Let's modify the code example above using typedef.

typedef MyTextPrinter = void Function(String value);

void exampleFunction(MyTextPrinter helloPrinter) {
  helloPrinter("Hello, world");
}

void examplePrinter(String value) {
  print(value);
}

exampleFunction(examplePrinter);

Same as before, the exampleFunction method will accept a function that accepts a string as a parameter as its parameter.

As long as the function signature matches what is defined in the typedef, it will be accepted by the exampleFunction.

When to use Closure in Programming

Closure is commonly used when we must perform an operation but need to determine which operation to execute based on external input. Consider a straightforward example:

💡
We want to create a calculator app. To do that, we need a function that calculates the result. It should accept three parameters. The first two parameters should be numbers, and the last one is the operation that the function should perform.

Let's create the function, we will call the function calculateResult like so:

typedef MyOperation = int Function(int a, int b);
int calculateResult(int a, int b, MyOperation myOperation){
	return myOperation(a, b);
}

Next, let's define the operations:

int add(int a, int b){
  return a + b;
}

int reduce(int a, int b){
  return a - b;
}

Next, we can just call the calculateResult function with parameters based on user input:

// user inputs
int first = 5;
int second= 7;
var theOperation = add;

// do calculate
int result = calculateResult(first, second, theOperation);

// view the result
print(result);

This is a very simple calculator function that we create, in reality, there is more than just add and reduce functionality. But at least, this example demonstrates one of the use cases to use closure in our code.

Conclusion

A closure is simply a function that is treated as a value, that can be saved into a variable, passed as an argument, or used as a return value. A typedef (type alias) is used to define a function signature.