Home > Net >  Flutter - The method 'where' isn't defined for the class 'DocumentReference<M
Flutter - The method 'where' isn't defined for the class 'DocumentReference<M

Time:05-28

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. but it gives me the error mentioned above. how can I implement the where statement without getting this error?

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,
                              ),
                            ),

                          ],
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            );
          }
      ),
    );
  }
}

CodePudding user response:

You need to remove the .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

CodePudding user response:

DocumentReference (https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/DocumentReference-class.html) returned by doc() doesn't have a method called where, but CollectionReference (https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/CollectionReference-class.html) returned by collection() does. You probably want to remove the doc() call between those.

  • Related