参考

https://dart.dev/guides/language/language-tour

正文

今天的文章简短地揭示了 dart 语言所提供的很酷的特性。更多时候,这些选项对于简单的应用程序是不必要的,但是当你想要通过简单、清晰和简洁来改进你的代码时,这些选项是一个救命稻草。

考虑到这一点,我们走吧。

cascade 级联

cascades (.., ?..) 允许你对同一个对象进行一系列操作。这通常节省了创建临时变量的步骤,并允许您编写更多流畅的代码。

var paint = paint();
paint.color = colors.black;
paint.strokecap = strokecap.round;
paint.strokewidth = 5.0;
//above block of code when optimized
var paint = paint()
  ..color = colors.black
  ..strokecap = strokecap.round
  ..strokewidth = 5.0;

abstract 抽象类

使用 abstract 修饰符定义一个 _abstract 抽象类(无法实例化的类)。抽象类对于定义接口非常有用,通常带有一些实现。

// this class is declared abstract and thus
// can't be instantiated.
abstract class abstractcontainer {
  // define constructors, fields, methods...
  void updatechildren(); // abstract method.
}

factory constructors 工厂建造者

在实现不总是创建类的新实例的构造函数时使用 factory 关键字。

class logger {
  string name;
  logger(this.name);
  factory logger.fromjson(map<string, object> json) {
    return logger(json['name'].tostring());
  }
}

named 命名构造函数

使用命名构造函数为一个类实现多个构造函数或者提供额外的清晰度:

class points {
  final double x;
  final double y;
  //unnamed constructor
  points(this.x, this.y);
  // named constructor
  points.origin(double x,double y)
      : x = x,
        y = y;
  // named constructor
  points.destination(double x,double y)
      : x = x,
        y = y;
}

mixins 混合物

mixin 是在多个类层次结构中重用类代码的一种方法。

要实现 implement mixin,创建一个声明没有构造函数的类。除非您希望 mixin 可以作为常规类使用,否则请使用 mixin 关键字而不是类。

若要使用 mixin,请使用后跟一个或多个 mixin 名称的 with 关键字。

若要限制可以使用 mixin 的类型,请使用 on 关键字指定所需的超类。

class musician {}
//creating a mixin
mixin feedback {
  void boo() {
    print('boooing');
  }
  void clap() {
    print('clapping');
  }
}
//only classes that extend or implement the musician class
//can use the mixin song
mixin song on musician {
  void play() {
    print('-------playing------');
  }
  void stop() {
    print('....stopping.....');
  }
}
//to use a mixin, use the with keyword followed by one or more mixin names
class performsong extends musician with feedback, song {
  //because performsong extends musician,
  //performsong can mix in song
  void awesomesong() {
    play();
    clap();
  }
  void badsong() {
    play();
    boo();
  }
}
void main() {
  performsong().awesomesong();
  performsong().stop();
  performsong().badsong();
}

typedefs

类型别名ー是指代类型的一种简明方式。通常用于创建在项目中经常使用的自定义类型。

typedef intlist = list<int>;
list<int> i1=[1,2,3]; // normal way.
intlist i2 = [1, 2, 3]; // same thing but shorter and clearer.
//type alias can have type parameters
typedef listmapper<x> = map<x, list<x>>;
map<string, list<string>> m1 = {}; // normal way.
listmapper<string> m2 = {}; // same thing but shorter and clearer.

extension 扩展方法

在 dart 2.7 中引入的扩展方法是一种向现有库和代码中添加功能的方法。

//extension to convert a string to a number
extension numberparsing on string {
  int customparseint() {
    return int.parse(this);
  }
  double customparsedouble() {
    return double.parse(this);
  }
}
void main() {
  //various ways to use the extension
  var d = '21'.customparsedouble();
  print(d);
  var i = numberparsing('20').customparseint();
  print(i);
}

可选的位置参数

通过将位置参数包装在方括号中,可以使位置参数成为可选参数。可选的位置参数在函数的参数列表中总是最后一个。除非您提供另一个默认值,否则它们的默认值为 null。

string joinwithcommas(int a, [int? b, int? c, int? d, int e = 100]) {
  var total = '$a';
  if (b != null) total = '$total,$b';
  if (c != null) total = '$total,$c';
  if (d != null) total = '$total,$d';
  total = '$total,$e';
  return total;
}
void main() {
  var result = joinwithcommas(1, 2);
  print(result);
}

unawaited_futures

当您想要启动一个 future 时,建议的方法是使用 unawaited

否则你不加 async 就不会执行了

import 'dart:async';
future dosomething() {
  return future.delayed(duration(seconds: 5));
}
void main() async {
  //the function is fired and awaited till completion
  await dosomething();
  // explicitly-ignored
  //the function is fired and forgotten
  unawaited(dosomething());
}

以上就是android开发dart语言7个很酷的特点的详细内容,更多关于android开发dart特点的资料请关注其它相关文章!