Dart | 你可能不知道的语言特性

1 概述

Dart 是单线程,兼有 AOT (Ahead of time)和 JIT (Just in time)编译,提供热重载机制的 UI 编程语言,也是当今正在发展的编程语言之一。它是著名的跨平台 UI 工具包 Flutter 的基本语言。Dart 以其与 JavaScript 语法极端相似而闻名,同时它也是一种强类型的“编译”语言。当然,它也可以编译成JavaScript。

虽然 Flutter 是一个非常强大的 UI 工具包,但是 Dart 的语法特性一直给开发者带来困惑和痛苦。好在,Dart新版本做了许多改进。实际上,它是继 JavaScript 之后最简单灵活的编程语言之一。不幸的是,我们中的很多人仍然不知道那些非凡的新特性,如果你现在使用的是最新版本,那么就赶快把它们应用起来吧。

2 运算符重载 / 重写

你有没有比较过两个Duration ?您可能使用了相等运算符 == 来实现这一点,对吗?但是如果你学过其他语言,比如 JS/Java/PHP,你会看到类似 Duration.isEqual 的东西。但是在Dart语言中,像大多数其他语言一样,也支持运算符重载。

class MyClass {
  final String value;

    MyClass(this.value);

  @override
  bool operator ==(other) {
    this.value == other.value;
  }
}

void main() {
  final a = MyClass("Hello");
  final b = MyClass("Hello");
  final c = MyClass("Hola")
  print(a == b); //result is true
  print(a == c); //result is false
}

您可以使用以上语法在 Dart 中重载每一个算术符或者比较运算符。

3 可选的匿名参数

您可能熟悉 Dart/Flutter 中的命名参数和可选参数。默认情况下,命名参数是可选的,您可以使用必要的关键字来设置它们。但是,你知道吗? Dart 也可以让匿名参数变成可选的。

void main(){
    example();
}

void example([String? name]){
    return "$name, it's just an owl!";
}

你可以使用带有默认值的可选匿名参数和命名参数,但是你不能同时使用命名参数和可选的匿名参数

void main(){
    what("Bustin");
}

// works fine
void what(String firstName, [String? lastName="Jeiber"]){
    return "Mr. $lastName, you're not $firstName";
}

// doesn't work
// syntax error. Expected ) after
void noway(bool flag, [String? wrong], {bool? isIt}){
    // .....
    // .....
}

// doesn't work
// syntax error. Expected ) after
void impossible(bool flag, {bool? isIt}, [String? wrong]){
    // .....
    // .....
}

4 super 参数

先看一个例子:

class CustomButton extends MaterialButton {
  const CustomButton({
    Key? key,
    required VoidCallback? onPressed,
    VoidCallback? onLongPress,
    ValueChanged<bool>? onHighlightChanged,
    MouseCursor? mouseCursor,
    ButtonTextTheme? textTheme,
    Color? textColor,
    Color? disabledTextColor,
    Color? color,
    Color? disabledColor,
    Color? focusColor,
    Color? hoverColor,
    Color? highlightColor,
    Color? splashColor,
    Brightness? colorBrightness,
    double? elevation,
    double? focusElevation,
    double? hoverElevation,
    double? highlightElevation,
    double? disabledElevation,
    EdgeInsetsGeometry? padding,
    VisualDensity? visualDensity,
    ShapeBorder? shape,
    Clip clipBehavior = Clip.none,
    FocusNode? focusNode,
    bool autofocus = false,
    MaterialTapTargetSize? materialTapTargetSize,
    Duration? animationDuration,
    Widget? child,
  }) : assert(autofocus != null),
       assert(elevation == null || elevation >= 0.0),
       assert(focusElevation == null || focusElevation >= 0.0),
       assert(hoverElevation == null || hoverElevation >= 0.0),
       assert(highlightElevation == null || highlightElevation >= 0.0),
       assert(disabledElevation == null || disabledElevation >= 0.0),
       assert(clipBehavior != null),
       super(
         key: key,
         onPressed: onPressed,
         onLongPress: onLongPress,
         onHighlightChanged: onHighlightChanged,
         mouseCursor: mouseCursor,
         textTheme: textTheme,
         textColor: textColor,
         disabledTextColor: disabledTextColor,
         color: color,
         disabledColor: disabledColor,
         focusColor: focusColor,
         hoverColor: hoverColor,
         highlightColor: highlightColor,
         splashColor: splashColor,
         colorBrightness: colorBrightness,
         elevation: elevation,
         focusElevation: focusElevation,
         hoverElevation: hoverElevation,
         highlightElevation: highlightElevation,
         disabledElevation: disabledElevation,
         padding: padding,
         visualDensity: visualDensity,
         shape: shape,
         clipBehavior: clipBehavior,
         focusNode: focusNode,
         autofocus: autofocus,
         materialTapTargetSize: materialTapTargetSize,
         animationDuration: animationDuration,
         child: child,
       );
// ..... other stuff .... //
}

之前,子类在创建构造函数式,必须把参数传递给父类的构造函数。结果,代码就会显得过于臃肿,如例子所示。、

幸运的是,从 Dart 2.17 稳定版开始,我们有了 super 参数。现在我们可以直接在子类构造函数里给父类传递参数。

class CustomButton extends MaterialButton {
  CustomButton({
    super.key,
    required super.onPressed,
    super.onLongPress,
    super.onHighlightChanged,
    super.mouseCursor,
    super.textTheme,
    super.textColor,
    super.disabledTextColor,
    super.color,
    super.disabledColor,
    super.focusColor,
    super.hoverColor,
    super.highlightColor,
    super.splashColor,
    super.colorBrightness,
    super.elevation,
    super.focusElevation,
    super.hoverElevation,
    super.highlightElevation,
    super.disabledElevation,
    super.padding,
    super.visualDensity,
    super.shape,
    super.none,
    super.focusNode,
    super.false,
    super.materialTapTargetSize,
    super.animationDuration,
    super.child,
  });
// ..... other stuff .... //
}

5 总结

尽管 Dart 编程语言仍然有很多缺陷,很多功能还待晚上。但是,它也在不断发展中。之前很多麻烦困难的工作,现在已经变得越来越简单。我希望随着时间的推移,这门语言能够增加更多的新功能、新特性,使其成为更加成熟、现代化的编程语言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孟华328

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值