我目前正在将我所有过时的代码迁移到最新版本的 firebase,当我更新 firebase 依赖项时,它给了我一个错误,即方法 '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);
}
}
最佳答案
在 FlutterFire 插件的 2.0 版本中,setData()
方法被重命名为 set()
。
对于此类情况,我发现将引用文档放在手边最有帮助,例如 DocumentReference
class在这种特定情况下。
关于firebase - 方法 'setData' 没有为类型 'documentreference' 定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68705735/