Home > Net >  A map functions? don't understand this dart
A map functions? don't understand this dart

Time:11-16

I see this source from google_fonts of dart pub. It seems we have a function map? and asMap() has a arrow function? Don't understand this.

static Map<
      String,
      TextStyle Function({
    TextStyle? textStyle,
    Color? color,
    Color? backgroundColor,
    double? fontSize,
    FontWeight? fontWeight,
    FontStyle? fontStyle,
    double? letterSpacing,
    double? wordSpacing,
    TextBaseline? textBaseline,
    double? height,
    Locale? locale,
    Paint? foreground,
    Paint? background,
    List<ui.Shadow>? shadows,
    List<ui.FontFeature>? fontFeatures,
    TextDecoration? decoration,
    Color? decorationColor,
    TextDecorationStyle? decorationStyle,
    double? decorationThickness,
  })> asMap() => const {
        'ABeeZee': GoogleFonts.aBeeZee,
        'Abel': GoogleFonts.abel}

CodePudding user response:

You're seeing a generated code, in the very beginning of the file:

// GENERATED CODE - DO NOT EDIT

// Copyright 2019 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

This function is defined as:

  • Name: asMap.
  • Scope: top-level (Since it is a static method).
  • Return type: Map<String, TextStyle Function({ ...args })>.

It seems we have a function map?

We have a function that returns a map of Strings and Closures. In Dart this is perfectly fine:

final Map<String, int Function(int, int)> operations = {
  'sum': (int a, int b) => a   b,
  'subtract': (int a, int b) => a - b,
  'multiply': (int a, int b) => a * b,
  'divide': (int a, int b) => a * b
};

operations['sum']!(30, 30); // 60
operations['subtract']!(30, 30); // 0
operations['multiply']!(30, 30); // 900
operations['divide']!(30, 30); // 1

In general this is not something we do by raw hands due to type implications (hard to maintain, you can see that it's pretty confusing it's pretty easy to lost track of type definitions and, consequently, all intellisense features). So that's why this is being done through code generation.

And in this particular case, we are generating a map, like this example below:

class Arithmetic {
  static int sum(int a, int b) {
    return a   b;
  }
  static int subtract(int a, int b) {
    return a - b;
  }
  static int multiply(int a, int b) {
    return a * b;
  }
  static int divide(int a, int b) {
    return a * b;
  }
  static Map<String, int Function(int, int)> asMap() {
    const Map<String, int Function(int, int)> operationsAsMap = {
      'sum': sum,
      'subtract': subtract,
      'multiply': multiply,
      'divide': divide
    };
    return operationsAsMap;
  }
}

asMap() has a arrow function?

No, the asMap() implementation is using an arrow function, which is an alias for:

// This is a shorthand...
static Map<...> asMap() => const { ... };

// For this:
static Map<...> asMap() {
  return const { ... };
}
  •  Tags:  
  • dart
  • Related