簡體   English   中英

獲取字段等於 flutter 中特定字符串的 Firestore 文檔

[英]Get Firestore documents with a field that is equal to a particular string in flutter

我正在嘗試獲取集合中具有等於特定字符串的特定字段的文檔。 我正在建立一個 POS,我想獲得特定城市的所有銷售額。

final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final CollectionReference _mainCollection = _firestore.collection('Sales');


  Stream<QuerySnapshot> readFeeds() {
    CollectionReference notesItemCollection =
    _mainCollection.where('seller_location', isEqualTo: "London").get();

    return notesItemCollection.snapshots();
  }

我收到此錯誤:

不能將“Future<QuerySnapshot<Object?>>”類型的值分配給“CollectionReference<Object?>”類型的變量。

我已將 added cast as CollectionReference<Object?>; 但查詢仍然無效。 這就是我訪問數據的方式:

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: readFeeds(),
      builder: (context, snapshot) {
        if (snapshot.hasError) {
          return Text('Something went wrong');
        } else if (snapshot.hasData || snapshot.data != null) {
          return ListView.separated(
            separatorBuilder: (context, index) => SizedBox(height: 16.0),
            itemCount: snapshot.data!.docs.length,
            itemBuilder: (context, index) {
              var noteInfo = snapshot.data!.docs[index];
              String docID = snapshot.data!.docs[index].id;
              String name = noteInfo['name'].toString();
              String price = noteInfo['price'].toString();
              String quantity = noteInfo['quantity'].toString();
              return Ink(
                decoration: BoxDecoration(
                  color: CustomColors.firebaseGrey.withOpacity(0.1),
                  borderRadius: BorderRadius.circular(8.0),
                ),
                child: ListTile(
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(8.0),
                  ),
                  onTap: () => Navigator.of(context).push(
                    MaterialPageRoute(
                      builder: (context) => EditScreen(
                        documentId: docID,
                        currentName: name,
                        currentPrice: price,
                        currentQuantity: quantity,
                      ),
                    ),
                  ),
                  title: Text(
                    name,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              );
            },
          );
        }
        return Center(
          child: CircularProgressIndicator(
            valueColor: AlwaysStoppedAnimation<Color>(
              CustomColors.firebaseOrange,
            ),
          ),
        );
      },
    );
  }
}

您收到以下錯誤:

不能將“Future<QuerySnapshot<Object?>>”類型的值分配給“CollectionReference<Object?>”類型的變量。

因為下面一行代碼:

CollectionReference notesItemCollection =
    _mainCollection.where('seller_location', isEqualTo: "London").get();

這是有道理的,因為get() function 返回一個Future不是CollectionReference object。在 Dart 中沒有辦法創建這樣的轉換,因此出現錯誤。

由於您使用的是where() function,因此返回的 object 的類型是Query 所以你的代碼應該是這樣的:

Query queryBySellerLocation =
    _mainCollection.where('seller_location', isEqualTo: "London");

正確定義此查詢后,您可以執行 get() 調用並收集結果:

queryBySellerLocation.get().then(...);

如果有幫助,請試試這個。

QuerySnapshot<Map<String,dynamic>>readFeeds() {
   QuerySnapshot<Map<String,dynamic>>response=await   _mainCollection.where('seller_location', isEqualTo: "London").get()

return response;
  }

你可以像這樣訪問這些數據

response.docs.forEach((element) {
        ///this is Map<String,dynamic>
        element.data();
      });

暫無
暫無

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

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