What is a typedef In Dart?

· 4 min read
What is a typedef In Dart
What is a typedef In Dart

A typedef, or function-type alias, gives a function type a name that you can use when declaring fields and return types. So in today’s article, we will walk through what is a typedef in Dart.

What is a typedef In Dart?

A typedef retains type information when a function type is assigned to a variable. A common usage pattern of typedef in Dart is defining a callback interface. For example:

typedef void LoggerOutputFunction(String msg);

class Logger {
  LoggerOutputFunction out;
  Logger() {
    out = print;
  }
  void log(String msg) {
    out(msg);
  }
}

void timestampLoggerOutputFunction(String msg) {
  String timeStamp = new Date.now().toString();
  print('${timeStamp}: $msg');
}

void main() {
  Logger l = new Logger();
  l.log('Hello World');
  l.out = timestampLoggerOutputFunction;
  l.log('Hello World');
}

Running the above sample yields the following output:

Hello World
2012-09-22 10:19:15.139: Hello World

The typedef line says that LoggerOutputFunction takes a String parameter and returns void.

timestampLoggerOutputFunction matches that definition and thus can be assigned to the outfield.

Dart 1.24 introduces a new typedef syntax to also support generic functions. The previous syntax is still supported.

For more details see https://github.com/dart-lang/sdk/blob/master/docs/language/informal/generic-function-type-alias.md

Function types can also be specified inline

void foo<T, S>(T Function(int, S) aFunction) {...}
typedef LoggerOutputFunction = void Function(String msg);

this looks much more clear than the previous version

Just slightly modified answer, according to the latest typedef syntax, The example could be updated to:

typedef LoggerOutputFunction = void Function(String msg);

class Logger {
  LoggerOutputFunction out;
  Logger() {
    out = print;
  }
  void log(String msg) {
    out(msg);
  }
}

void timestampLoggerOutputFunction(String msg) {
  String timeStamp = new Date.now().toString();
  print('${timeStamp}: $msg');
}

void main() {
  Logger l = new Logger();
  l.log('Hello World');
  l.out = timestampLoggerOutputFunction;
  l.log('Hello World');
}

Typedef in Dart is used to create a user-defined function (alias) for other application functions,

Syntax: typedef function_name (parameters);

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

Syntax:typedef variable_name = function_name;

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

Syntax: variable_name(parameters);

Example:

// Defining alias name
typedef MainFunction(int a, int b);

functionOne(int a, int b) {
  print("This is FunctionOne");
  print("$a and $b are lucky numbers !!");
}

functionTwo(int a, int b) {
  print("This is FunctionTwo");
  print("$a + $b is equal to ${a + b}.");
}

// Main Function
void main() {
  // use alias
  MainFunction number = functionOne;

  number(1, 2);

  number = functionTwo;
 // Calling number
  number(3, 4);
}

Output:

This is FunctionOne
1 and 2 are lucky numbers !!
This is FunctionTwo
3 + 4 is equal to 7

Up next, We’ll see type alias dart, typedef list, object, dart generics, type, class, map.

Conclusion:

Thanks for being with us on a flutter journey !!!

So If you are still confused about flutter development…Do let us know!! We’d love to assist

Keep Learning !!! Keep Fluttering !!!

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.

Leave a Reply