【问题标题】:Why im getting error on QuerySnapShot firestore flutter?为什么我在 QuerySnapShot firestore 颤动时遇到错误?
【发布时间】:2021-07-14 06:10:15
【问题描述】:

我尝试使用 Flutter 和 Firestore 创建一个社交应用。 当我尝试在模拟器上运行我的应用程序时,我收到这样的错误。 “QuerySnapshot”类没有实例获取器“文档”。 接收方:“QuerySnapshot”实例 尝试调用:文档

如何解决这个错误?我是颤振和火力的新手。我能感觉到,我的编码无法从云火库中读取文档。但我不知道如何清除它。

我的代码

import 'package:flutter/material.dart';
import 'package:mysuroo_in/widgets/indicators.dart';

typedef ItemBuilder<T> = Widget Function(
  BuildContext context,
  DocumentSnapshot doc,
);

class StreamBuilderWrapper extends StatelessWidget {
  final Stream<dynamic> stream;
  final ItemBuilder<DocumentSnapshot> itemBuilder;
  final Axis scrollDirection;
  final bool shrinkWrap;
  final ScrollPhysics physics;
  final EdgeInsets padding;

  const StreamBuilderWrapper({
    Key key,
    @required this.stream,
    @required this.itemBuilder,
    this.scrollDirection = Axis.vertical,
    this.shrinkWrap = false,
    this.physics = const ClampingScrollPhysics(),
    this.padding = const EdgeInsets.only(bottom: 2.0, left: 2.0),
  }) : super(key: key);


//Got error here
  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: stream,
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          var list = snapshot.data.documents.toList();
          return list.length == 0
              ? Padding(
                  padding: const EdgeInsets.only(top: 100.0),
                  child: Container(
                    height: 60.0,
                    width: 100.0,
                    child: Center(
                      child: Text('No Posts'),
                    ),
                  ),
                )
              : ListView.builder(
                  padding: padding,
                  scrollDirection: scrollDirection,
                  itemCount: list.length,
                  shrinkWrap: shrinkWrap,
                  physics: physics,
                  itemBuilder: (BuildContext context, int index) {
                    return itemBuilder(context, list[index]);
                  },
                );
        } else {
          return circularProgress(context);
        }
      },
    );
  }
}

Error image

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    尝试使用

     var list = snapshot.data['documents'].toList();
    

    因为 data 参数是一个地图

    或者如果它不适合你使用这个

    var list = snapshot.data()['documents'].toList();
    

    【讨论】:

    • 很抱歉,如果我使用您的两种解决方案,则会出现同样的错误。但无论如何谢谢你兄弟..
    【解决方案2】:

    如果你在 Flutter 中使用新的 Firestore 库,那么你必须使用,

    var list = snapshot.data.docs.toList()
    

    而不是使用

    var list = snapshot.data.documents.toList();
    

    在您的流构建器中。

    【讨论】:

      【解决方案3】:

      您可以从新的 firebase 版本调用

      docs() instead of documents()
      

      之前

      var list = snapshot.data.documents.toList();
      

      之后

      QuerySnapshot snap = snapshot.data;
      var list = snap.docs;
      //now you will have list of documents in the list
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-23
        • 2020-08-03
        • 2016-05-03
        • 2021-08-17
        • 1970-01-01
        相关资源
        最近更新 更多