问题描述
我正在测试一些Flutter应用程序并遇到:在Dart Function中,它的用途是什么?
I am testing some Flutter application and encounter : in a Dart Function, What is it used for?
我在Firebase for Flutter教程中找到了它。我试图在Google中搜索它,但找不到任何内容。
I found it in Firebase for Flutter tutorial. I tried to search it in google, but can't find anything.
class Record {
final String name;
final int votes;
final DocumentReference reference;
Record.fromMap(Map<String, dynamic> map, {this.reference})
: assert(map['name'] != null),
assert(map['votes'] != null),
name = map['name'],
votes = map['votes'];
Record.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, reference: snapshot.reference);
@override
String toString() => "Record<$name:$votes>";
}
结果运行良好,我只想知道什么是:二手
The Result is working fine, i just want to know what is the : used for.
推荐答案
从,构造函数部分:
- Superclass
class Employee extends Person {
Employee() : super.fromJson(getDefaultData());
// ···
}
- 实例变量
// Initializer list sets instance variables before
// the constructor body runs.
Point.fromJson(Map<String, num> json)
: x = json['x'],
y = json['y'] {
print('In Point.fromJson(): ($x, $y)');
}
您的示例:
因此,在您的情况下, fromMap
-Method会做一些断言(在运行构造函数之前)并为变量 name赋值
和投票
。由于这些变量是最终变量,因此必须在初始化记录实例之前对其进行分配!
So in your case the fromMap
-Method does some assertions (before running the constructor) and assigns the variables name
and vote
. As these variables are final, they have to be assigned before initializing a record instance!
fromSnapshot
仅使用 fromMap
作为超级构造函数。
fromSnapshot
just uses fromMap
as super constructor.
这篇关于:在Dart函数中有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!