Cam*_*son 5 dart firebase flutter google-cloud-firestore
我目前正在将所有过时的代码迁移到最新版本的 firebase,当我更新 firebase 依赖项时,它给了我一个错误,即没有为“documentreference”类型定义方法“setData”。
class DatabaseService {
final String uid;
DatabaseService({ this.uid });
// Collection reference
// final CollectionReference brewCollection = FirebaseFirestore.instance.collection('brews');
final CollectionReference<Map<String, dynamic>> brewCollection = FirebaseFirestore.instance.collection('brews');
Future updateUserData(String sugars, String name, int strength) async {
return await brewCollection.doc(uid).setData({
'sugars' : sugars,
'name' : name,
'strength' : strength
});
}
// Brew list from snapshot
List<Brew> _brewListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.docs.map((doc){
return Brew(
name: doc.data['name'] ?? '',
strength: doc.data['strength'] ?? 0,
sugars: doc.data['sugars'] ?? '0'
);
}).toList();
}
// Get brews stream
Stream<List<Brew>> get brews {
return brewCollection.snapshots().map(_brewListFromSnapshot);
}
UserData _userDataFromSnapshot(DocumentSnapshot snapshot) {
return UserData(
uid: uid,
name: snapshot.data['name'],
sugars: snapshot.data['sugars'],
strength: snapshot.data['strength']
);
}
// Get user document
Stream<UserData> get userData {
return brewCollection.doc(uid).snapshots().map(_userDataFromSnapshot);
}
}
Run Code Online (Sandbox Code Playgroud)
该方法在 FlutterFire 插件 2.0 版本中setData()已重命名。set()
对于这样的情况,我发现将参考文档放在手边是最有帮助的,例如这个特定情况下的DocumentReference类。
| 归档时间: |
|
| 查看次数: |
3442 次 |
| 最近记录: |