简体   繁体   English

有人可以向我解释 fromMap() 和 fromSnapshot()

[英]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.

相关问题 当我可以在客户端代码中逐字编写规则时,有人可以向我解释为什么我们需要 Firebase 安全规则吗? - Can someone please explain to me why we need Firebase security rules when I can literally write rules in my client code? 有人可以解释一下这个。/已经存在/.test(err?message) 方法吗? - Can someone explain this !/already exists/.test(err.message) method? 我无法解决此错误。 有人能帮我吗? - I can't solve this error. Can someone help me? 有人可以帮助我解决成功监听器上的 Firebase 逻辑吗 - Can someone help me with logic of the firebase on success listener 有人可以解释为什么$ firebaseArray和$ extend的这种Angularjs情况不起作用吗? - Can someone explain why this Angularjs situation with $firebaseArray and $extend won't work? 有人可以清楚地解释.Value,.ChildAdded,.ChildChanged,.ChildRemoved for FIRDataEventType之间的区别吗? - Can someone clearly explain difference between .Value, .ChildAdded, .ChildChanged, .ChildRemoved for FIRDataEventType? Flutter firebase 来自地图 - Flutter firebase fromMap 有人可以帮我吗 - FirebaseError:collection() 的第一个参数应该是 CollectionReference、DocumentReference 或 FirebaseFirestore? - Can someone help me - FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore? 有人可以帮我解决这个错误吗,由于某种原因 Provider 无法识别 - Can someone help me with this error, for some reason Provider isn't recognized 在dart中将Map毫秒毫秒SinceEpoch转换为DateTime - Converting fromMap millisecondsSinceEpoch to DateTime in dart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM