Flutter(四):Flutter 语言 Dart基础使用
一、变量和常量
var a = 1;
var str = 'abc';
常量
const PI = 3.14;
不可变对象
final PI = 3.14;
final TIME = new DateTime.now()
二、流程控制语句
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);
四、注释
/// 这是一个文档注释。
/// 文档注释用于为库、类以及类的成员添加注释。
/// 像 IDE 和 dartdoc 这样的工具可以专门处理文档注释。
/* 也可以像这样使用单斜杠和星号的注释方式 */
五、导入( Import )
标准
// 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';
指定库前缀(解决代码库冲突)
import 'package:lib1/lib1.dart';
import 'package:lib2/lib2.dart' as lib2;
// Uses Element from lib1.
Element element1 = Element();
// Uses Element from lib2.
lib2.Element element2 = lib2.Element();
导入库的一部分
// Import only foo.
import 'package:lib1/lib1.dart' show foo;
// Import all names EXCEPT foo.
import 'package:lib2/lib2.dart' hide foo;
六、类( Class )
普通
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.
}
// Method.
void describe() {
// code doing
}
}
继承
// 继承
class Orbiter extends Spacecraft {
double altitude;
Orbiter(super.name, DateTime super.launchDate, this.altitude);
}
抽象类
abstract class Describable {
void describe();
void describeWithEmphasis() {
print('=========');
describe();
print('=========');
}
}
Mixins
Mixin 是一种复用多个类之间的代码,减少耦合。下面的是声明一个 Mixin 的做法:
mixin Piloted {
int astronauts = 1;
void describeCrew() {
print('Number of astronauts: $astronauts');
}
}
class PilotedCraft extends Spacecraft with Piloted {
// ···
}
可以将多个mixins放入同一个类中,而且dart对这个数量没有作任何限制。
七、接口
Dart 没有 interface 关键字。相反,所有的类都隐式定义了一个接口。因此,任意类都可以作为接口被实现。
class MockSpaceship implements Spacecraft {
// ···
}
八、枚举(Enums)
enum PlanetType { terrestrial, gas, ice }
九、异常(try/catch)
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();
}
异步(async/await )
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');
}
}
}
你可能想看: