类
成员变量赋值
1 2 3 4 5 6 7 8 9
| class MyClass { final Color color;
MyClass(this.color);
void describeColor() { print('The color is: $color'); } }
|
super
1 2 3 4 5 6 7 8 9 10 11
| class Parent { int a; Parent(this.a); }
class Child extends Parent { int b; Child(int a, this.b) : super(a) { } }
|
字典传参
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
| class User { int id; String? name; String password; String? email; String? phone; String address; String avatar; String introduction; Gender? gender; DateTime? birthday;
UserStatus status; UserType type; DateTime createdAt; DateTime? updatedAt;
User({ required this.id, this.name, required this.password, this.email, this.phone, this.address = "", this.avatar = "", this.introduction = "", this.gender, this.birthday, this.status = UserStatus.using, this.type = UserType.unknown, required this.createdAt, this.updatedAt, }); }
|
非空类型赋值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class SingleTask extends StatefulWidget { String? taskTitle, taskRelativePerson; DateTime? taskDate; SingleTask({ Key? key, this.taskTitle, this.taskDate, this.taskRelativePerson, }) : super(key: key) { taskTitle ??= ""; taskRelativePerson ??= ""; print("$taskTitle, $taskRelativePerson, $taskDate"); }
@override State<SingleTask> createState() => _SingleTaskState(); }
|
调试
打印
1
| print("$taskTitle, $taskRelativePerson, $taskDate");
|
map 方法
在 Dart 中,map
方法是一个非常强大的工具,用于对集合(如列表或可迭代对象)中的每个元素执行一个函数,并将结果收集为一个新的可迭代对象。这种用法广泛应用于数据转换、过滤、和操作场景中。
map
方法的使用
如下代码:
1
| subTask.map((task) => taskPlane(task)).toList(),
|
这里发生的事情是:
subTask
是一个可迭代的集合,比如 List。
.map((task) => taskPlane(task))
对 subTask
集合中的每一个元素执行 taskPlane(task)
函数。task
是当前迭代到的元素。
.toList()
是将 map
操作的结果(仍然是一个可迭代对象)转换成一个列表。
最佳实践
使用箭头函数简化代码(当适用时):如果转换逻辑非常简单,可以直接使用箭头函数,使代码更加简洁。
明确类型注解:尽管 Dart 能够推断出类型,明确地声明输入和输出的类型可以提高代码的可读性和维护性。
1
| List<NewType> newList = subTask.map<NewType>((Task task) => taskPlane(task)).toList();
|
避免不必要的性能损耗:.toList()
会创建一个新的集合,如果只需要迭代结果,不一定需要转换成 List,直接使用 .map()
的结果进行迭代可以节省资源。
结合使用其他迭代方法:map
经常与 where
(过滤)、any
(检查是否至少有一个元素满足条件)、fold
(累加器)等方法结合使用,以实现更复杂的数据处理逻辑。
理解 map
和 forEach
的区别:map
用于转换集合中的每个元素并收集结果,而 forEach
仅用于遍历集合中的元素执行操作,不返回任何结果。
使用 map
的目的是生成一个新的集合,其中包含对原始集合中每个元素应用给定函数后的结果。它是函数式编程的一个体现,可以让写出更清晰、更简洁、更易于维护的代码。
语法糖
??=
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class TaskTitle extends StatelessWidget { String? title; TaskTitle({Key? key, this.title}) : super(key: key) { title ??= ""; }
@override Widget build(BuildContext context) { return Expanded( child: Text( title!, style: const TextStyle(fontWeight: FontWeight.bold), ), ); } }
|
断言
在Dart中,断言(assertions)是一种在开发过程中用于检查代码逻辑的工具,它可以确保变量或表达式符合特定条件。如果断言失败(即条件评估为false
),程序会抛出一个AssertionError
异常。这在开发和测试阶段非常有用,因为它可以帮助开发者及早发现和修复潜在的错误。然而,需要注意的是,断言只在开发模式下工作,在生产模式下会被忽略。
1 2 3 4 5 6 7 8 9 10 11
| void main() { var text = 'Dart'; assert(text.isNotEmpty, 'The text should not be empty.');
assert(text.length > 3, 'The text should be more than 3 characters.'); print('Passed all assertions.'); }
|