Dart示例代码

3周前 63次点击 来自 移动端

标签: DartFlutter

原文地址:编写高质量Dart程序(Dart2)

变量

var name = 'Voyager I';
var year = 1977;
var antennaDiameter = 3.7;
var flybyObjects = ['Jupiter', 'Saturn', 'Uranus', 'Neptune'];
var image = {
  'tags': ['saturn'],
  'url': '//path/to/saturn.jpg'
};

控制流程语句

if (year >= 2001) {
  print('21st century');
} else if (year >= 1901) {
  print('20th century');
}

for (var object in flybyObjects) {
  print(object);
}

for (int month = 1; month <= 12; month++) {
  print(month);
}

while (year < 2016) {
  year += 1;
}

函数

int fibonacci(int n) {
  if (n == 0 || n == 1) return n;
  return fibonacci(n - 1) + fibonacci(n - 2);
}

var result = fibonacci(20);

import

// Importing core libraries
import 'dart:async';
import 'dart:math';

// Importing libraries from external packages
import 'package:test/test.dart';

// Importing files
import 'path/to/my_other_file.dart';
import '../lib/samples/spacecraft.dart';

这是一个具有三个属性,两个构造函数和一个方法的类的示例。其中一个属性无法直接设置,因此使用getter方法(而不是变量)定义。

class Spacecraft {
  String name;
  DateTime launchDate;

  // Constructor, with syntactic sugar for assignment to members.
  Spacecraft(this.name, this.launchDate) {
    // Initialization code goes here.
  }

  // Named constructor that forwards to the default one.
  Spacecraft.unlaunched(String name) : this(name, null);

  int get launchYear =>
      launchDate?.year; // read-only non-final property

  // Method.
  void describe() {
    print('Spacecraft: $name');
    if (launchDate != null) {
      int years =
          DateTime.now().difference(launchDate).inDays ~/
              365;
      print('Launched: $launchYear ($years years ago)');
    } else {
      print('Unlaunched');
    }
  }
}

您可以使用这样的Spacecraft类:

var voyager = Spacecraft('Voyager I', DateTime(1977, 9, 5));
voyager.describe();

var voyager3 = Spacecraft.unlaunched('Voyager III');
voyager3.describe();

继承

class Orbiter extends Spacecraft {
  num altitude;
  Orbiter(String name, DateTime launchDate, this.altitude)
      : super(name, launchDate);
}

Mixins

Mixins是一种在多个类层次结构中重用代码的方法。以下类可以充当mixin:

class Piloted {
  int astronauts = 1;
  void describeCrew() {
    print('Number of astronauts: $astronauts');
  }
}

要将mixin的功能添加到类中,只需使用mixin扩展类。

class PilotedCraft extends Spacecraft with Piloted {
  // ···
}

接口和抽象类

Dart没有接口关键字。相反,所有类都隐式地定义一个接口。因此,您可以实现任何类。

class MockSpaceship implements Spacecraft {
  // ···
}

您可以创建一个抽象类,由一个具体类扩展(或实现)。抽象类可以包含抽象方法(带有空主体)。

abstract class Describable {
  void describe();

  void describeWithEmphasis() {
    print('=========');
    describe();
    print('=========');
  }
}

任何类扩展描述都有describe()方法,它调用extender的describe()实现。

异步

避免回调地狱,并通过使用异步和wait使代码更具可读性。

const oneSecond = Duration(seconds: 1);
// ···
Future<Null> printWithDelay(String message) async {
  await Future.delayed(oneSecond);
  print(message);
}

上述方法相当于:

Future<Null> printWithDelay(String message) {
  return Future.delayed(oneSecond).then((_) {
    print(message);
  });
}

如下一个示例所示,async和await会帮助使异步代码易于阅读。

Future<Null> createDescriptions(Iterable<String> objects) async {
  for (var object in objects) {
    try {
      var file = File('$object.txt');
      if (await file.exists()) {
        var modified = await file.lastModified();
        print(
            'File for $object already exists. It was modified on $modified.');
        continue;
      }
      await file.create();
      await file.writeAsString('Start describing $object in this file.');
    } on IOException catch (e) {
      print('Cannot create description for $object: $e');
    }
  }
}

您还可以使用async*,它提供了一种很好的、可读的方式来构建流。

Stream<String> report(Spacecraft craft, Iterable<String> objects) async* {
  for (var object in objects) {
    await Future.delayed(oneSecond);
    yield '${craft.name} flies by $object';
  }
}

异常

要引发异常,使用throw:

if (astronauts == 0) {
  throw StateError('No astronauts.');
}

要捕获异常,使用带有on或catch(或两者)的try语句:

try {
  for (var object in flybyObjects) {
    var description = await File('$object.txt').readAsString();
    print(description);
  }
} on IOException catch (e) {
  print('Could not describe object: $e');
} finally {
  flybyObjects.clear();
}
Card image cap
开发者雷

尘世间一个小小的开发者,每天增加一些无聊的知识,就不会无聊了

要加油~~~

技术文档 >> 系列应用 >>
热推应用
Let'sLearnSwift
学习Swift的入门教程
PyPie
Python is as good as Pie
标签