firebase - Flutter/Dart/FireStore 查询错误:NoSuchMethod
问题描述
我有一个查询firestore和检索特定用户的功能,但我一直收到同样的错误。
我得到的错误如下:
I/flutter (29600): The following NoSuchMethodError was thrown building SellerInfo(dirty, state:
I/flutter (29600): _SellerInfoState#90e11):
I/flutter (29600): Class 'Future<User>' has no instance getter 'displayName'.
I/flutter (29600): Receiver: Instance of 'Future<User>'
I/flutter (29600): Tried calling: displayName
I/flutter (29600):
我首先尝试了这个:
Future<User> getUser(String displayName) async{
await Firestore.instance.collection('users')
.where('displayName', isEqualTo: displayName)
.snapshots()
.listen( (QuerySnapshot onData) {
List<User> user = onData.documents.map(
(DocumentSnapshot doc) => _fromUserSnapShot(doc)
);
return user.first;
});
return null;
}
接着
Future<User> getUser(String displayName) async{
return (await Firestore.instance.collection('users')
.where('displayName', isEqualTo: displayName)
.getDocuments())
.documents
.map((DocumentSnapshot doc) => _fromUserSnapShot(doc))
.first;
}
这是用户类
class User {
final String displayName;
final String email;
final String school;
User({
@required this.displayName,
@required this.email,
@required this.school,
});
}
解决方案
尝试直接获取该用户的文档:Firestore.instance.collection('users').document(displayName).get()
推荐阅读
- redirect - 在外部文件中重写规则 nginx
- python - 我将如何在 mongoDB 中存储相关的表格数据?
- python - Unix 上的 Python :: os.EX_NOTFOUND 的值
- scala - 使用 Scala 以优化的方式将 Spark 数据帧的 Not null 值替换为“1”
- google-maps - 谷歌距离矩阵 API。如何访问替代路线?
- javascript - 在进度条上激活下一步
- azure - msal.js loginRedirect() 反复重定向到登录窗口 react.js
- hibernate - 在 REST Api 中使用 JPA 规范进行 SQL 注入
- javascript - 使用 javascript 从数据集中获取傻瓜
- python - 缺少值的 SMOTE