![](/img/trans.png)
[英]Flutter The method 'User' isn't defined for the type '_LeaveApplicationState'
[英]Flutter FireStore: The method 'where' isn't defined for the type 'DocumentReference'
如果我使用代码,我可以正确接收数据。 但是,使用.where
时,出现错误:
没有为类型“DocumentReference”定义方法“where”。 尝试将名称更正为现有方法的名称,或定义名为“where”的方法。
代码:
chamaBancoDeDados(curso, materia, id, dificuldade) {
Map<String, dynamic> dados = {};
final documentoRef = FirebaseFirestore.instance
.collection('cursos')
.doc(curso)
.collection(materia)
.doc(id)
.where('dificuldade', arrayContainsAny: ['normal']);
documentoRef
.get()
.then((DocumentSnapshot documento) {
if (documento.exists) {
try {
dados = documento.data() as Map<String, dynamic>;
print("dados são: $dados");
} on StateError catch (e) {
print('Erro tentando adquirir o documento no banco de dados: $e');
}
} else {
print('O documento não existe no banco de dados');
}
});
return dados;
}
}
我已经在 inte.net 上搜索了很多,包括 Firebase 文档,但我不知道哪里出错了。
如果您在代码的line 7
看到,它从 firestore 获取文档,并且您不能在文档上使用.where()
。
.where()
用于 collections 检查所有文档(在该集合中)的条件并返回满足需要的文档。
根据上面给出的信息,我建议您删除7th line
代码并重试。
请分享您的 Firestore 层次结构,以便我们建议更好的方法来执行您的查询。
您当前的代码尝试读取具有已知 ID 的单个文档,在这种情况下,无法使用where
子句进一步过滤数据。 因此,如果您只想阅读该单个文档,请删除where
:
final documentoRef = FirebaseFirestore.instance
.collection('cursos')
.doc(curso)
.collection(materia)
.doc(id);
另一方面,如果您想以正常难度级别读取 `` 集合中的所有文档,可以通过删除.doc(id)
子句来完成:
final documentoRef = FirebaseFirestore.instance
.collection('cursos')
.doc(curso)
.collection(materia)
.where('dificuldade', arrayContainsAny: ['normal']);
我做了这个并按我想要的方式工作:
final teste = FirebaseFirestore.instance
.collection('cursos')
.doc(curso)
.collection(materia)
.where('dificuldade', arrayContainsAny: [dificuldade]);
await teste
.get()
.then((event) {
print("${doc.id} => ${doc.data()}");
}
}, onError: (e) => print("Error completing: $e"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.