Programming in Dart: Functions & Closures

Jun 21 2022 · Dart 2.16, Flutter, DartPad

Part 1: Meet the Function

07. Understand Typedef

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 06. Store a Function Next episode: 08. Use Arrow Notation

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the last episode, we passed a function to a function and in doing so, we created a function signature that was a little complex and unwieldy. Dart allows to use an alias when referring to functions and other types. In such a way, it allows to create a shorthand alias so instead of using a clunky method signature, you use the alias instead. You can typedef used throughout the Flutter codebase.

int multiply(int a, int b) {
    return a * b;
}
int processScores(List<int> scores, int Function(int, int) processor) {
    var total = 0;
    for (var score in scores) {
        total += processor(score, 2);
    }
    return total;
}
void main() {
  var scores = [54, 75, 32];
  print(processScores(scores, multiply));
}
typedef
typedef ScoreModifier
typedef ScoreModifier = int Function(int, int);
int processScores(List<int> scores, ScoreModifier processor) {