【问题标题】:type 'Query' is not a subtype of type 'Stream<QuerySnapshot>?'“Query”类型不是“Stream<QuerySnapshot>?”类型的子类型
【发布时间】:2021-07-28 02:15:32
【问题描述】:

在 Flutter 中,我有一个称为供应商的集合,我将根据其集合的图像来获取集合

这是我的代码:

class _TopPickStoreState extends State<TopPickStore>{
StoreService _storeServices = StoreService();
@override
Widget build(BuildContext context){
   return Container(
   child: StreamBuilder<QuerySnapshot>(
     stream: _storeServices.getTopPickedStore(),
     builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapShot){
      if(!snapShot.hasData)return CircularProgressIndicator();
      return Column(
        children: [
          Flexible(
              child: ListView(
                scrollDirection: Axis.horizontal,
                children: snapShot.data.docs.map((DocumentSnapshot document){
                  return Container(
                    width: 80,
                    child: Column(
                      children: [
                        Image.network(document['imageUrl']),
                      ]
                    ),
                  )
                ;}).toList(),
              ),
          )
        ],
      );
    }
    ),
);

} }

这是获取集合的函数:

import 'package:cloud_firestore/cloud_firestore.dart';


 class StoreService{
     getTopPickedStore(){
       return FirebaseFirestore.instance.collection('vendors')
       .where('accVerified', isEqualTo:true)
       .where('isTopPicked',isEqualTo: true)
       .orderBy('shopName');
     }
   }

我收到了这个错误信息:

类型 'Query' 不是类型 'Stream

谁能帮我解决这个问题?

【问题讨论】:

  • 问题是由getTopPickedStore()引起的,发布这个函数。因为它返回query 对象,但您的流构建器是查询快照。决定你要使用哪一个,然后就可以修复了
  • 我已经放了getTopPickedStore()函数,希望你能详细解释一下如何解决这个问题。非常感谢
  • 我添加了一个答案。基本上,函数中当前写入的内容是查询。这意味着它是一组说明,告诉 Firebase 在哪里以及要查找什么,但您不告诉它之后要做什么。必须告诉get()snapshots(),才能真正进入firebase,获取这些文档,然后将它们作为查询快照返回。

标签: flutter dart firebase-authentication syntax-error snapshot


【解决方案1】:

改成这样:

getTopPickedStore() async {
       return await FirebaseFirestore.instance.collection('vendors')
       .where('accVerified', isEqualTo:true)
       .where('isTopPicked',isEqualTo: true)
       .orderBy('shopName').snapshots();

您需要使用快照将其更改为查询快照。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-12
    • 2019-10-24
    • 2021-03-09
    • 2020-07-02
    • 2021-12-30
    • 1970-01-01
    • 2021-04-13
    • 2020-08-23
    相关资源
    最近更新 更多