[英]Can someone explain fromMap() and fromSnapshot() to me
Can someone explain the below code to me?I was learning cloud firestore in flutter and i am not able to understand below fromMap conecpt and fromSnapshot conept有人可以向我解释下面的代码吗?我正在学习 flutter 中的 cloud firestore,但我无法理解下面的 fromMap concpt 和 fromSnapshot conept
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>";
}
fromMap()
The data which received from API is in JSON format, so it's a key-value pair relationship and for that map is used. fromMap()
从 API 接收到的数据是 JSON 格式的,所以它是一个键值对关系,用于该映射。 And it's used basically to parse the value from the map and assign it to local variables in the model它基本上用于解析地图中的值并将其分配给模型中的局部变量
Example:例子:
name = map['name'],
votes = map['votes'];
Above both variables are accessible from model object instance.以上两个变量都可以从模型对象实例访问。
fromSnapshot():
It's similar to fromMap()
, the only difference is that it's giving its values in DocumentSnapshot, and call fromMap()
fromSnapshot():
类似于fromMap()
,唯一的区别是它在 DocumentSnapshot 中给出它的值,并调用fromMap()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.