Flutter Flatter FIRESTORE-查询字段并获取文档id并检查其他字段

Flutter Flatter FIRESTORE-查询字段并获取文档id并检查其他字段,flutter,google-cloud-firestore,document,exists,snapshot,Flutter,Google Cloud Firestore,Document,Exists,Snapshot,我正在尝试获取firestore中文档字段中的搜索值(userid)。我想检查文档的其他字段(状态)。我尝试了此方法,但失败 handlesubmit(BuildContext context)async{ final QuerySnapshot searcheduserid= await Firestore.instance.collection('users') .where('userid',isEqualTo: userid).li

我正在尝试获取firestore中文档字段中的搜索值(userid)。我想检查文档的其他字段(状态)。我尝试了此方法,但失败

handlesubmit(BuildContext context)async{

final QuerySnapshot searcheduserid=
               await Firestore.instance.collection('users')
            .where('userid',isEqualTo: userid).limit(1).getDocuments();

final userdocid=searcheduserid.documents.map((doc)=>doc.documentID);

final DocumentSnapshot getuserdoc= await Firestore.instance.collection('users')
                      .document(userdocid).get();

final userstatus = getuserdoc.data['status'];
//  I GET AN ERROR HERE ERROR SAYS

// METHOD [](status) was called on null

    if(userstatus==null){
        return showdialog( context,'the entered user id status does not exist');
    }
}

您可能复制了较旧版本的代码,因为您的代码不太可能编译程序的以下行:

final DocumentSnapshot getuserdoc= await Firestore
    .instance
    .collection('users')
    .document(userdocid).get();
我的系统上的错误消息是:

无法将参数类型“
Iterable
”分配给参数类型“
String

这意味着
userdocid
String
类型的
Iterable
Iterable
),但需要
String
类型的参数

您有多种解决问题的方法,但我建议如下: 由于您只需要从
QuerySnapshot
查看第一个文档就足够了

final QuerySnapshot searchedUserId = await Firestore.instance
    .collection('users')
    .where('userid', isEqualTo: userid)
    .limit(1)
    .getDocuments();
// if it is possible that searchedUserId returns no document make sure to 
// check whether searchedUserId.documents.length > 0, 
// otherwise searchedUserId.documents.first will throw an error
DocumentSnapshot document = searchedUserId.documents.first;
final userDocId = document.documentID;
final DocumentSnapshot getuserdoc =
    await Firestore.instance.collection('users').document(userDocId).get();
改进的解决方案:

不过,我认为您的代码有点多余,因为您正在查找与文档ID具有相同值的文档字段。您可以将整个代码缩短为

final DocumentSnapshot getuserdoc =
        await Firestore.instance.collection('users').document(userid).get();
调用getuserdoc.data['status']时出错

如果你在说类似的话时出错

对null调用了
[](状态)

这意味着
getuserdoc
没有值。这可能是因为没有具有给定ID的此类数据库条目。请检查该ID是否在您的数据库中,否则请在下面进行注释以获取更多信息,因为提供的代码有编译错误,根本无法运行