0%

【Flutter】Dart学习笔记

Dart 学习笔记

基本概念

  1. 一切皆对象,包括 functions、null,所有对象都继承自Object
  2. dart 具有变量类型推导功能
  3. dart 支持泛型 List
  4. 没有publicprotectedprivate关键字,以_开头的表示库级私有
  5. 未初始化的所有变量都有初始值为null
  6. 支持finalconst,const 在编译时确定值
  7. lexically scope 语言

内建类型

  • numbers (int、double)
  • strings (UTF-16)
  • booleans
  • lists
  • sets
  • maps
  • runes
  • symbols

dart 2.3

lists,sets,maps 新增操作......?

1
2
3
4
5
6
7
var list = [1, 2, 3];
var list2 = [0, ...list];
assert(list2.length == 4);

var list;
var list2 = [0, ...?list];
assert(list2.length == 1);

lists,sets,maps 新增collection ifcollection for

1
2
3
4
5
6
7
8
9
10
11
var nav = [
'home',
'Plants',
if (promoActive) 'Outlet'
];

var listOfInts = [1, 2, 3];
var listOfStrings = [
'#0',
for (var i in listOfInts) '#$i'
];

Functions

命名参数

1
2
3
4
// 将参数列表用{}包裹来定义命名参数
void enableFlags({bool bold, bool hidden}) {...}
// 调用
enableFlags(bold: true, hidden: false);

命名参数同时是可选参数,如果要必须则需要加@required

1
void enableFlags({@required bool bold, bool hidden}) {...}

可选且具有特定顺序的参数

1
2
3
4
5
6
7
String say(String from, String msg, [String device]) {
var result = '$from says $msg';
if (device != null) {
result = '$result with a $device';
}
return result;
}

参数默认值

1
void enableFlags({bool bold = false, bool hidden}) {...}

级联调用

1
2
3
4
5
void main() {
querySelector('#smale')
..text = 'Click me'
..onClick.listen(reverseText);
}

错误处理

Dart 所有的异常都是未检异常即方法不声明具体抛出哪些异常,调用者也不要求必须检查这些异常。

Dart 预定义了ExceptionError类型,但是你也可以使用 throw 抛出任何非空类型

1
2
3
4
5
6
7
8
9
10
try {
//...
} on Exception catch (e) {
print('Exception details:\n $e');
} catch (e, s) {
print('Exception details:\n $e');
print('Stack trace:\n $s');
} finally {
clean();
}

Classes

  1. 如果没有提供构造函数,则默认提供一个无参构造函数
  2. 一个类可以实现多个接口(抽象类)

callable classes
实现call()方法能让一个类的实例可以像函数一样被调用。

1
2
3
4
5
6
7
8
class WannabeFunction {
String call(String a, String b,String c) => '$a $b $c';
}

var wf = WannabeFunction();
var out = wf('Hi', 'there', 'gang');

main() => print(out);

构造函数

命名构造函数

1
2
3
4
5
6
7
8
9
10
11
class Point {
num x, y;

Point(this.x, this.y);

// named constructor
Point.origin() {
x = 0;
y = 0;
}
}

调用父类构造函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Person {
String firstName;

Person.fromJson(Map data) {
print('in Person');
}
}

class Employee extends Person {
// Person does not have a default constructor
// you must call super.fromJson(data).
Employee.fromJson(Map data) : super.fromJson(data) {
print('in employee');
}
}

注意: 在“:”后的语句无法访问 this

构造函数初始化列表

1
2
3
4
5
// Initializer list sets instance variables before
// ths constructor body runs.
Point.fromJson(Map<String, num> json) : x = json['x'], y = json['y'] {
print('In point.fromJson(): ($x, $y)');
}

构造函数参数断言(assert)

1
2
3
Point.withAssert(this.x, this.y): assert(x >= 0) {
print('In Point.withAssert(): ($x, $y)');
}

构造函数重定向

1
2
3
4
5
6
7
8
9
class Point {
num x, y;

// The main constructor for this class.
Point(this.x, this.y);

// Delegates to the main constructor.
Point.alongXAixs(num x): this(x, 0);
}

常量构造函数
如果你的对象一旦被实例化后就不再改变,那可以让类在编译时常量化。

1
2
3
4
5
6
7
class ImmutablePoint {
static final ImmutablePoint origin = const ImmutablePoint(0, 0);

final num x, y;

const ImmutablePoint(this.x, this.y);
}

工厂构造函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Logger {
final String name;
bool mute = false;

// _cache is library-private, thanks to the _ in front ot its name.
static final Map<String, Logger> _cache = <String, Logger>{};

// Factory constructor have no access to `this`.
factory Logger(String name) {
return _cache.putIfAbsent(name, () => Logger._internal(name));
}

Logger._internal(this.name);

void log(String msg) {
if (!mute) print(msg);
}
}

运算符重载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Vector {
final int x, y;

Vector(this.x, this.y);

Vector operator +(Vector v) => Vector(x + v.x, y + v.y);
Vector operator -(Vector v) => Vector(x - v.x, y - v.y);
}

void main() {
final v = Vector(2, 3);
final w = Vector(2, 2);

assert(v + w == Vector(4, 5));
assert(v - w == Vector(0, 1));
}
请吃个烤串~~
willsky 微信

微信