Open In App

Typedef in Dart

Last Updated : 11 Apr, 2025
Suggest changes
Share
21 Likes
Like
Report

Typedef in Dart is used to create a user-defined identity (alias) for a function, and we can use that identity in place of the function in the program code. In this article, we will learn about Typedef in Dart.

Why Use typedef?

  • Enhances Readability: Simplifies complex function types for easier understanding.
  • Promotes Reusability: Facilitates the definition of versatile function signatures.
  • Type-Safe Parameter Passing: Allows for clean and safe function parameterization.
  • Ideal for Callbacks: Perfect for working with callbacks, event handlers, and functional programming patterns.

How to Use Typedef with Dart Functions?

There are a few cases that tell us how to use typedef in Dart. Cases are mentioned below:

1. When we use typedef, we can define the parameters of the function.

typedef function_name ( parameters );

2. With the help of typedef, we can also assign a variable to a function.

typedef variable_name = function_name;

3. After assigning the variable, if we have to invoke it, then we go as:

variable_name( parameters );

By this, we will be able to use a single function in different ways:

Programs to Illustrate the Use of typedef

Using typedef

Example: 

Dart
// Dart program to show the usage of typedef // Defining alias name typedef GeeksForGeeks(int a, int b); // Defining Geek1 function Geek1(int a, int b) {  print("This is Geek1");  print("$a and $b are lucky geek numbers !!"); } // Defining Geek2 function Geek2(int a, int b) {  print("This is Geek2");  print("$a + $b is equal to ${a + b}."); } // Main Function void main() {  // Using alias name to define  // number with Geek1 function  GeeksForGeeks number = Geek1;  // Calling number  number(1, 2);    // Redefining number  // with Geek2 function  number = Geek2;  // Calling number  number(3, 4); } 

Output:

This is Geek1
1 and 2 are lucky geek numbers !!
This is Geek2
3 + 4 is equal to 7.

Note: Apart from this, typedef can also act as parameters of a function.


Using typedef as a parameter of a function

Example:

Dart
// Dart program to show the usage of typedef // Defining alias name typedef GeeksForGeeks(int a, int b); // Defining Geek1 function Geek1(int a, int b) {  print("This is Geek1");  print("$a and $b are lucky geek numbers !!"); } // Defining a function with a typedef variable number(int a, int b, GeeksForGeeks geek) {  print("Welcome to GeeksForGeeks");  geek(a, b); } // Main Function void main() {  // Calling number function  number(21, 23, Geek1); } 

Output:

Welcome to GeeksForGeeks
This is Geek1
21 and 23 are lucky geek numbers !!

Note: typedefs were restricted to function types before the 2.13 version of Dart


Typedef for Variables and Collections

Typedef is not restricted to functions; we can use variables and collections. Let us check the syntax for this:

typedef name = Collection;

Example:

Dart
// Dart program to show the usage of typedef // Defining alias name typedef List_Integer = List<int>; // Main Function void main() {    // Using alias name to Declare  // List of Integer type  List_Integer x=[11,21,31];    // Print the List  print(x); } 

Output:

[11 , 21, 31]



Article Tags :

Explore