美文网首页
Dart简介

Dart简介

作者: 木易林1 | 来源:发表于2023-09-05 08:47 被阅读0次

Dart 简介

本文会通过示例向你展示 Dart 语言用法的简单的介绍。

想要深度学习 Dart 语言,请访问左侧栏 语言 的具体内容。

想要了解 Dart 的核心库,访问 Library tour。你也可以查看 Dart 语言的速查表 CodeLab 来获得一些快速上手的指导。

Hello World

每个应用都有一个顶层的 main() 函数来作为运行入口。没有指定返回类型的方法的返回类型会推导为 void。你可以使用顶层函数 print() 来将一段文本输出显示到控制台:

void main() {
  print('Hello, World!');
}

变量

虽然 Dart 是 代码类型安全 的语言,你仍然可以用 var 来定义变量,而不用显式指定它们的类型。由于其支持类型推断,因此大多数变量的类型会由它们的初始化内容决定:

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'
};

流程控制语句

Dart 支持常用的流程控制语句:

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

for (final 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);

=> (胖箭头) 简写语法用于仅包含一条语句的函数。该语法在将匿名函数作为参数传递时非常有用:

flybyObjects.where((name) => name.contains('turn')).forEach(print);

上面的示例除了向你展示了匿名函数(上例中传入 where() 函数的参数即是一个匿名函数)外,还向你展示了将函数作为参数使用的方式:上面示例将顶层函数 print() 作为参数传给了 forEach()函数。

你可以 阅读更多 Dart 中有关函数的内容,包括可选参数、默认参数值以及词法作用域。

注释

Dart 通常使用双斜杠 // 作为注释的开始。

/// doc comments specially.

/* Comments like these are also supported. */

导入 (Import)

使用 import 关键字来访问在其它库中定义的 API。

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

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

// Importing files
import 'path/to/my_other_file.dart';

类 (Class)

下面的示例中向你展示了一个包含三个属性、两个构造函数以及一个方法的类。其中一个属性不能直接赋值,因此它被定义为一个 getter 方法(而不是变量)。该方法使用字符串插值来打印字符串文字内变量的字符串。

class Spacecraft {
  String name;
  DateTime? launchDate;

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

  // 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);

  // Method.
  void describe() {
    print('Spacecraft: $name');
    // Type promotion doesn't work on getters.
    var launchDate = this.launchDate;
    if (launchDate != null) {
      int years = DateTime.now().difference(launchDate).inDays ~/ 365;
      print('Launched: $launchYear ($years years ago)');
    } else {
      print('Unlaunched');
    }
  }
}

枚举类型 (Enum)

枚举类型的取值范围是一组预定义的值或实例。

下面这个简单的枚举示例定义了一组行星类别:

enum PlanetType { terrestrial, gas, ice }

下面是一个增强型枚举的示例,定义了一组行星类的常量实例,即太阳系的行星:

/// Enum that enumerates the different planets in our solar system
/// and some of their properties.
enum Planet {
  mercury(planetType: PlanetType.terrestrial, moons: 0, hasRings: false),
  venus(planetType: PlanetType.terrestrial, moons: 0, hasRings: false),
  // ···
  uranus(planetType: PlanetType.ice, moons: 27, hasRings: true),
  neptune(planetType: PlanetType.ice, moons: 14, hasRings: true);

  /// A constant generating constructor
  const Planet(
      {required this.planetType, required this.moons, required this.hasRings});

  /// All instance variables are final
  final PlanetType planetType;
  final int moons;
  final bool hasRings;

  /// Enhanced enums support getters and other methods
  bool get isGiant =>
      planetType == PlanetType.gas || planetType == PlanetType.ice;
}

你可以这样使用 Planet 枚举:

final yourPlanet = Planet.earth;

if (!yourPlanet.isGiant) {
  print('Your planet is not a "giant planet".');
}

扩展类(继承)

Dart 支持单继承。

class Orbiter extends Spacecraft {
  double altitude;

  Orbiter(super.name, DateTime super.launchDate, this.altitude);
}

你可以 阅读更多 Dart 中有关类继承的内容,比如可选的 @override 注解等等。

Mixins

Mixin 是一种在多个类层次结构中重用代码的方法。下面的是声明一个 Mixin 的做法:

mixin Piloted {
  int astronauts = 1;

  void describeCrew() {
    print('Number of astronauts: $astronauts');
  }
}

现在你只需使用 Mixin 的方式继承这个类就可将该类中的功能添加给其它类。

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

接口和抽象类

所有的类都隐式定义成了一个接口。因此,任意类都可以作为接口被实现。

class MockSpaceship implements Spacecraft {
  // ···
}

你可以阅读更多关于 隐式接口 或者 interface 关键词 的内容。

你可以创建一个被任意具体类扩展(或实现)的抽象类。抽象类可以包含抽象方法(不含方法体的方法)。

abstract class Describable {
  void describe();

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

任意一个扩展了 Describable 的类都拥有 describeWithEmphasis() 方法,这个方法又会去调用实现类中实现的 describe() 方法。

异步

使用 asyncawait 关键字可以让你避免回调地狱 (Callback Hell) 并使你的代码更具可读性。

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

上面的方法相当于:

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

如下一个示例所示,asyncawait 关键字有助于使异步代码变得易于阅读。

Future<void> createDescriptions(Iterable<String> objects) async {
  for (final 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。

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

异常

使用 throw 关键字抛出一个异常:

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

使用 try 语句配合 oncatch(两者也可同时使用)关键字来捕获一个异常:

Future<void> describeFlybyObjects(List<String> flybyObjects) async {
  try {
    for (final 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();
  }
}

注意上述代码是异步的;同步代码以及异步函数中得代码都可以使用 try 捕获异常。

相关文章

  • Flutter 代码规范整理

    Dart 语法简介 Flutter是使用Dart语言开发的。Dart语言是基于类的纯面向对象语言。Dart 中的所...

  • 2019-04-20

    Dart语言简介 https://book.flutterchina.club/chapter1/dart.htm...

  • Flutter-Dart基础语法入门

    Dart语法基础 Dart语言简介 Dart是Google推出的一门编程语言,最初是希望取代Javascript运...

  • Dart 笔记 1 - 基础

    参考: Dart2 中文文档 Dart 语言简介 两个空格作为缩进。语句结束需要分号。 print 在 dart:...

  • Dart - Dart 语言介绍

    在线练习 Dart 在线练习 - DartPad 一些文章 Dart 开发语言概览Dart语言简介 30分钟掌握D...

  • Dart语言简介

    最新Dart2.7版本 mixins,代码重用方法。PilotedCraft拥有Piloted的所有字段和方法 接...

  • Dart语言简介

    1.3 Dart语言简介 在跨平台开领域被 JS 一统天下的今天,Dart 语言的出现无疑是一股清流。作为后来者,...

  • Dart语言简介

    学习使用,研究完就删原文地址Dart语言简介在之前我们已经介绍过Dart语言的相关特性,读者可以翻看一下,如果你熟...

  • Dart语言简介

    Dart的设计目标应该是既对标Java,也对标JavaScript,Dart在静态语法方面和Java非常相似,如类...

  • Dart语言简介

    Dart 初体验 我们在Android Studio中建立一个main.dart文件,右键Run main.dar...

网友评论

      本文标题:Dart简介

      本文链接:https://www.haomeiwen.com/subject/xsfhvdtx.html