【问题标题】:Get Firestore documents with a field that is equal to a particular string in flutter获取具有等于flutter中特定字符串的字段的Firestore文档
【发布时间】:2021-11-27 11:13:32
【问题描述】:

我正在尝试获取集合中具有等于特定字符串的特定字段的文档。我正在构建一个 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>”类型的值不能分配给“CollectionReference”类型的变量。

我已经添加了演员as CollectionReference&lt;Object?&gt;;,但查询仍然无法正常工作。这就是我访问数据的方式:

  @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,
            ),
          ),
        );
      },
    );
  }
}

【问题讨论】:

    标签: firebase flutter dart google-cloud-platform google-cloud-firestore


    【解决方案1】:

    您收到以下错误:

    “Future>”类型的值不能分配给“CollectionReference”类型的变量。

    因为下面这行代码:

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

    这是有道理的,因为get() 函数返回一个Future不是一个CollectionReference 对象。 Dart 中没有办法创建这样的转换,因此会出现错误。

    由于您使用的是where() 函数,因此返回的对象类型为Query。所以你的代码应该是这样的:

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

    正确定义此查询后,您可以执行 get() 调用并收集结果:

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

    【讨论】:

      【解决方案2】:

      如果有帮助,请尝试一下。

      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();
            });
      

      【讨论】:

        猜你喜欢
        • 2019-12-06
        • 2022-11-22
        • 2021-08-25
        • 2018-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-28
        • 2020-09-20
        相关资源
        最近更新 更多