Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Understand Typedef

This video was last updated on Jun 21 2022

Learn how to define an alias to functions to make your them easier to both use & read.

Contributors

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

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) {

Reviews

Comments