簡體   English   中英

Flutter - 沒有為類“DocumentReference”定義方法“where” <Map<String, dynamic> &gt;&#39;

[英]Flutter - The method 'where' isn't defined for the class 'DocumentReference<Map<String, dynamic>>'

我試圖初始化'where'以便它從firestore讀取,我想從文檔中讀取(用戶數)等於20的所有文檔,然后將它們顯示在屏幕上。 但它給了我上面提到的錯誤。 如何在不出現此錯誤的情況下實現 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,
                              ),
                            ),

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

您需要刪除 .doc(DocID): _firestore.collection('Community').where("numberOfMembers",isEqualTo: 20).get()

使用 .doc(DocID) 您只選擇一個文檔,因此在這種情況下沒有意義

doc()返回的DocumentReference沒有名為where的方法,但collection()返回的CollectionReference有。 您可能希望刪除它們之間的doc()調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM