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 exactly is a typedef in Dart, and how can it benefit your code? In this informative blog post, we’ll delve into the concept of typedef in Dart and explore its role in defining custom types. Understanding typedefs will not only enhance your code readability but also simplify complex type signatures and promote reusability. Whether you’re new to Dart or an experienced developer, this article will provide valuable insights and practical examples to help you harness the power of typedefs in your Dart projects. Don’t miss out on this opportunity to level up your Dart programming skills.

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 !!!

Abhishek Dhanani

Written by Abhishek Dhanani

Abhishek Dhanani, a skilled software developer with 3+ years of experience, masters Dart, JavaScript, TypeScript, and frameworks like Flutter and NodeJS. Proficient in MySQL, Firebase, and cloud platforms AWS and GCP, he delivers innovative digital solutions.

Leave a comment

Your email address will not be published. Required fields are marked *


Discuss Your Project

Connect with Flutter Agency's proficient skilled team for your app development projects across different technologies. We'd love to hear from you! Fill out the form below to discuss your project.

Have Project For Us

Get in Touch

"*" indicates required fields

ready to get started?

Fill out the form below and we will be in touch soon!

"*" indicates required fields