How to create a type alias in Flutter?

Viewed 1528

I have seen some old questions/answers that said it's impossible to create a type alias on Flutter. I just want to make sure whether it's the case, as the language seems to have been updated numerous times since.

My specific question is, how can I make a type alias like this:

typealias Json = Map<String, dynamic>;

?

Or is there any workaround, because I've tried to use empty mixin to do this but it says that there are 18 missing method implementations.

3 Answers

Small update on Randai Schwartz's comment. The feature seems (as of April 2021) almost ready and will probably be released in the next minor version or the one after it.

When you read this, it is probably worth checking the following issue: https://github.com/dart-lang/language/issues/65

This should allow to do:

typedef NewTypeName = OldTypeName;

Update:

Flutter officially supports now typedefs. :) So you can just use my code example above.

Flutter 2.2 has been released together with Dart 2.13, now SimonEritsch code should work as you wanted as long as you run flutter upgrade on your terminal or upgrade your dart.

Related