1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import 'package:ch03_types_collections/collections.dart';
import 'package:ch03_types_collections/generics.dart';
import 'package:ch03_types_collections/money.dart';
import 'package:ch03_types_collections/null_safety.dart';
import 'package:ch03_types_collections/shapes.dart';
void main() {
print('--- 1. 值类型:Money ---');
const coffee = Money(2050);
final tea = Money.fromYuan(20.5);
print('coffee = $coffee, tea = $tea');
print('coffee == tea ? ${coffee == tea} identical? ${identical(coffee, tea)}');
print('coffee + tea = ${coffee + tea}');
print('放进 Set 会被去重: ${<Money>{coffee, tea, Money(100)}.length} 个');
print('\n--- 2. 增强枚举 ---');
for (final p in Planet.values) {
print('${p.label}: g = ${p.surfaceGravity.toStringAsFixed(2)} m/s²');
}
print('\n--- 3. sealed 类 + 穷尽 switch ---');
const shapes = <Shape2>[
Circle(1),
Rectangle(3, 4),
Rectangle.square(2),
Triangle(6, 5),
];
for (final s in shapes) {
print('${label(s)} → ${s.area.toStringAsFixed(2)}');
}
print('总面积 = ${totalArea(shapes).toStringAsFixed(2)}');
const named = NamedCircle(2, '大圆');
print('${named.describe()} · compareTo: ${named.compareTo(const NamedCircle(1, '小圆'))}');
print('\n--- 4. 泛型 ---');
print('maxOf([3,1,4,1,5]) = ${maxOf(<int>[3, 1, 4, 1, 5])}');
print('maxOf(["pear","apple"]) = ${maxOf(<String>['pear', 'apple'])}');
print('Box(2).map(x*10) = ${const Box<int>(2).map((int x) => x * 10)}');
print('Pair(1,"a").swapped = ${const Pair<String, int>('a', 1).swapped}');
print('isIntList(<int>[1]) = ${isIntList(<int>[1])}, isIntList(<String>["1"]) = ${isIntList(<String>['1'])}');
print('sumOf([1,2,3]) = ${sumOf(<int>[1, 2, 3], seed: 0)}');
print('sumOf([1.5,2.5]) = ${sumOf(<double>[1.5, 2.5], seed: 0.0)}');
try {
addDouble(widenToNum(<int>[1, 2]));
} on TypeError catch (e) {
print('协变写入被运行期拦下: ${e.runtimeType}');
}
print('\n--- 5. 集合 ---');
print('偶数的平方: ${squaresOfEven(<int>[1, 2, 3, 4, 5, 6])}');
print('按长度分组: ${groupByLength(<String>['go', 'dart', 'c', 'rust', 'flutter'])}');
print('词频: ${wordCounts('the quick the lazy dog the')}');
print('去重: ${uniqueWords('Go go DART dart C')}');
print('降序: ${sortedDescending(<int>[3, 1, 4, 1, 5])}');
print('range(5) = ${range(5)}, fixedSlots = ${fixedSlots()}');
print('tags = ${buildTags(starred: true, extra: <String>['a'], more: <String>['b'])}');
print('inventory = ${inventory()}');
print('\n--- 6. null 安全 ---');
print('displayName(null) = ${displayName(null)}');
print('displayName(" Ada ") = ${displayName(' Ada ')}');
print('shout(null) = ${shout(null)}, shout("hi") = ${shout('hi')}');
print('firstWord(" ") = ${firstWord(' ')}');
print('cityOfUser(嵌套 map) = ${cityOfUser(<String, Object?>{
'address': <String, Object?>{'city': '上海'},
})}');
print('cityOfUser(null) = ${cityOfUser(null)}');
final cfg = LazyConfig();
print('dbPath = ${cfg.dbPath}, 计算次数 = ${cfg.computeCount}');
print('再次读取 dbPath = ${cfg.dbPath}, 计算次数 = ${cfg.computeCount}');
try {
uninitializedLate();
} catch (e) {
print('late 未初始化: ${e.runtimeType}');
}
try {
unsafeBang(null);
} on TypeError catch (e) {
print('! 遇到 null: ${e.runtimeType}');
}
print('lookup(不存在) = ${lookup(<String, int>{'a': 1}, 'b')}');
}