[英]Flutter - The method 'where' isn't defined for the class 'DocumentReference<Map<String, dynamic>>'
I tried to initialize the 'where' so that it will read from the firestore, I want to read all the documents that have the (number of users) equal to 20 from the document and then display them on the screen.我试图初始化'where'以便它从firestore读取,我想从文档中读取(用户数)等于20的所有文档,然后将它们显示在屏幕上。 but it gives me the error mentioned above.
但它给了我上面提到的错误。 how can I implement the where statement without getting this error?
如何在不出现此错误的情况下实现 where 语句?
class _communityFinderState extends State<communityFinder> {
late QuerySnapshot communityFinder;
FirebaseFirestore _firestore = FirebaseFirestore.instance;
bool _loading = false;
Future getCommunities()async{
setState(() {
_loading = true;
});
communityFinder = await _firestore.collection('Community').doc('com').where("numberOfMembers",isEqualTo: 20).get();
setState(() {
_loading = false;
});
}
@override
void initState() {
// TODO: implement initState
super.initState();
getCommunities();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _loading ? Center(child: CircularProgressIndicator()) : ListView.builder(
shrinkWrap: true,
itemCount: communityFinder.docs.length,
itemBuilder: (BuildContext context, int index) {
return Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
SizedBox(
height: MediaQuery.of(context).size.width *0.05,
),
InkWell(
child: Container(
width: MediaQuery.of(context).size.width *0.80,
height: MediaQuery.of(context).size.width *0.15,
decoration: BoxDecoration(
border: Border.all(
color: Colors.blue,
),
color: Colors.black12,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
children: [
SizedBox(
width: MediaQuery.of(context).size.width *0.04,
),
Text(
communityFinder.docs[index].get("CommunityName").toString(),
style: TextStyle(
color: Colors.black,
fontSize: MediaQuery.of(context).size.width *0.05,
),
),
],
),
],
),
),
),
],
);
}
),
);
}
}
You need to remove the .doc(DocID): _firestore.collection('Community').where("numberOfMembers",isEqualTo: 20).get()您需要删除 .doc(DocID): _firestore.collection('Community').where("numberOfMembers",isEqualTo: 20).get()
With .doc(DocID) you select only one document so it doesn't make sense in this case使用 .doc(DocID) 您只选择一个文档,因此在这种情况下没有意义
DocumentReference
returned by doc()
doesn't have a method called where
, but CollectionReference
returned by collection()
does. doc()
返回的DocumentReference
没有名为where
的方法,但collection()
返回的CollectionReference
有。 You probably want to remove the doc()
call between those.您可能希望删除它们之间的
doc()
调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.