首页 > 解决方案 > 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,
    });
}

标签: firebasedartgoogle-cloud-firestoreflutter

解决方案


尝试直接获取该用户的文档:Firestore.instance.collection('users').document(displayName).get()


推荐阅读