【问题标题】:Cannot understand this constructor syntax in Dart无法理解 Dart 中的这种构造函数语法
【发布时间】:2019-08-04 07:19:13
【问题描述】:

我正在学习 Udacity 的 Flutter 课程。 对于一些任务,我正在学习如何创建小部件。 来自 C++/Python,我根本无法理解此类构造函数的语法。

所以我的 main.dart 在中心小部件内包含一个类别小部件(我正在构建的那个)。我从 main.dart 文件传递​​了 3 个参数,但我不明白

const Category({...}) : ... ;

部分正在做。

这是我的 category.dart 的样子:

import 'package:flutter/material.dart';

class Category extends StatelessWidget {
  final String name;
  final ColorSwatch color;
  final IconData iconLocation;

  const Category({
    Key key,
    @required this.name,
    @required this.color,
    @required this.iconLocation,
  })  : assert(name != null),
        assert(color != null),
        assert(iconLocation != null),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 100.0,
      padding: EdgeInsets.all(8.0),
      child: InkWell(
        borderRadius: BorderRadius.circular(25.0),
        splashColor: color,
        onTap: () {
          print('i am cool');
        },
        child: Row(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.all(16.0),
              child: Icon(
                iconLocation, 
                size: 60.0,
              ),
            ),
            Text(
              'Length',
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 24.0,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    在 C++、Python 或 Java 中看不到一些特定于 Dart 的语言特性。您可以在Dart documentation 中阅读有关它们的更多信息。

    命名参数

    在 Python 中,每个参数都可能是可命名的。在 Dart 中,只有用大括号括起来的参数才能在构造函数调用中通过它们的名称来引用。因此,在参数周围加上大括号可以让我们在调用方看起来很漂亮:

    Category(
      something: ...
      somethingElse: ...
    )
    

    初始化列表

    初始化列表位于构造函数签名和主体之间。 它在身体之前运行。在那里,您可以初始化实例变量、断言事物并调用 super。

    你可能会问,为什么我们不能在构造函数体中做这些事情? 看看下一个功能:

    常量表达式

    如果一个构造函数满足几个要求(比如它没有构造函数体并且类只有最终字段),则可以将其标记为常量。这些附加要求允许将构造函数的调用标记为const,从而使构造函数在编译时而不是在运行时运行。 如果您经常重复使用特定实例(例如,EdgeInsets.all(16)),所有实例将共享相同的内存位置。因此标有const 的构造函数允许在编译时将类直接嵌入到结果程序的内存中。

    【讨论】:

      【解决方案2】:

      我从未编写过飞镖。 但这看起来与 java 构造函数非常相似。

      class Category extends StatelessWidget {
        final String name;
        final ColorSwatch color;
        final IconData iconLocation;
      
      const Category({
          Key key,
          @required this.name,
          @required this.color,
          @required this.iconLocation,
        })  : assert(name != null),
              assert(color != null),
              assert(iconLocation != null),
              super(key: key);
      

      构造函数通常用于初始化类属性(除其他外)。这个需要三个参数,我假设 dart 自动将具有匹配名称的构造函数参数分配给相应的类属性。它还有一些验证规则,例如 name != null 等。

      【讨论】:

      • 正确。 '{}' 中的变量被命名为参数。因此,在您的构造函数中,您将其分配为名称:名称。当变量前面没有@required 时,显然在创建对象时包含此参数是可选的。
      猜你喜欢
      • 2019-09-25
      • 2016-04-02
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      • 1970-01-01
      • 2020-08-25
      相关资源
      最近更新 更多