Firebase 在颤振省道应用中获得错误。运算符';[]和#x27;isn';t为类型';文档快照';。尝试定义运算符';[]和#x27;
这是我的密码。我尝试改用data(),但也没用Firebase 在颤振省道应用中获得错误。运算符';[]和#x27;isn';t为类型';文档快照';。尝试定义运算符';[]和#x27;,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,这是我的密码。我尝试改用data(),但也没用 Future getUserInterests(userId) async { User currentUser = User(); await _firestore.collection('users').doc(userId).get().then((user) { currentUser.name = user['name']; currentUser.photo = user['photoUrl
Future getUserInterests(userId) async {
User currentUser = User();
await _firestore.collection('users').doc(userId).get().then((user) {
currentUser.name = user['name'];
currentUser.photo = user['photoUrl'];
currentUser.gender = user['gender'];
currentUser.interestedIn = user['interestedIn'];
});
return currentUser;
}
您需要调用以获取数据库中的数据
Map data=user.data();
currentUser.name=数据['name'];
您运行的firebase软件包版本是什么?新版本分别为方法get()
和data()
更改了运算符[]
和getterdata
Future getUserInterests(userId) async {
User currentUser = User();
await _firestore.collection('users').doc(userId).get().then((user) {
Map<String, dynamic> doc = user.data();
currentUser.name = doc['name'];
currentUser.photo = doc['photoUrl'];
currentUser.gender = doc['gender'];
currentUser.interestedIn = doc['interestedIn'];
});
return currentUser;
}
Future-getUserInterests(userId)异步{
User currentUser=User();
等待_firestore.collection('users').doc(userId.get()。然后((user){
Map doc=user.data();
currentUser.name=doc['name'];
currentUser.photo=doc['photoUrl'];
currentUser.gender=doc['gender'];
currentUser.interestedIn=doc['interestedIn'];
});
返回当前用户;
}
好吧,您将用户
变量视为一个映射
(使用[]
访问器表示法),但错误告诉您它是文档快照
的一个实例
不确定DocumentSnapshot
有哪些字段(您正在使用哪个库?)。您应该查看所使用的任何库的文档,以了解如何访问所需的字段
Future getUserInterests(userId) async {
User currentUser = User();
await _firestore.collection('users').doc(userId).get().then((user) {
Map<String, dynamic> doc = user.data();
currentUser.name = doc['name'];
currentUser.photo = doc['photoUrl'];
currentUser.gender = doc['gender'];
currentUser.interestedIn = doc['interestedIn'];
});
return currentUser;
}