【问题标题】:Wrapping a CustomScrollView and a Sliverlist with a Streambuilder throws exception用 Streambuilder 包装 CustomScrollView 和 Sliverlist 会引发异常
【发布时间】:2021-08-01 08:15:06
【问题描述】:

我正在尝试使用 Streambuilder 从 Firebase firestore 监听实时文档,同时在 CustomScrollViewSliverList 中显示检索到的数据,但是当我这样做时,列表中断并且屏幕保持空白。

我尝试了FutureBuilder,它成功了,但它不能满足我的要求。

这是我为流构建器编写的代码:

Widget sliverList() {
  return CustomScrollView(
    slivers: <Widget>[
      SliverAppBar(
        backgroundColor: Colors.transparent,
        expandedHeight: 220.0,
        flexibleSpace: FlexibleSpaceBar(
          collapseMode: CollapseMode.parallax,
          background: Column(
            children: [Daily(), AddPost()],
          ),
        ),
      ),
      StreamBuilder<QuerySnapshot>(
          stream: feedServices.listenToFeedinStream(),
          builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
            return SliverFixedExtentList(
              itemExtent: 500,
              delegate: SliverChildBuilderDelegate(
                (BuildContext context, int index) {
                  if (snapshot.hasData) {
                    Post post = Post.fromMap(snapshot.data.docs[index].data());
                    return Column(
                      children: [
                        FeedItem(
                          thread: post,
                        ),
                        seperator()
                      ],
                    );
                  } else if (!snapshot.hasData) {
                    return Text('no data');
                  } else if (snapshot.hasError) {
                    return Text('has err');
                  } else {
                    return Text('not specified');
                  }
                },
                childCount: snapshot.data.docs.length,
              ),
            );
          })
    ],
  );
}

捕获的异常:

flutter: The following NoSuchMethodError was thrown building StreamBuilder<QuerySnapshot>(dirty, state:
flutter: _StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot<QuerySnapshot>>#da6d3):
flutter: The getter 'docs' was called on null.
flutter: Receiver: null
flutter: Tried calling: docs

【问题讨论】:

  • 请提供异常信息。
  • @hacker1024 考虑查看更新的问题,我添加了捕获的异常

标签: flutter dart stream-builder flutter-sliver customscrollview


【解决方案1】:

您应该只将 Sliver 小部件传递给 slivers 列表。 StreamBuilder 不是 Sliver,所以你可以用 StreamBuilder 包裹整个 CustomScrollView

Widget sliverList() {
  return StreamBuilder<QuerySnapshot>(
    stream: feedServices.listenToFeedinStream(),
    builder: (context, snapshot) {
      return CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            backgroundColor: Colors.transparent,
            expandedHeight: 220.0,
            flexibleSpace: FlexibleSpaceBar(
              collapseMode: CollapseMode.parallax,
              background: Column(
                children: [Daily(), AddPost()],
              ),
            ),
          ),
          SliverFixedExtentList(
              itemExtent: 500,
              delegate: SliverChildBuilderDelegate(
                (BuildContext context, int index) {
                  if (snapshot.hasData) {
                    Post post = Post.fromMap(snapshot.data.docs[index].data());
                    return Column(
                      children: [
                        FeedItem(
                          thread: post,
                        ),
                        seperator()
                      ],
                    );
                  } else if (!snapshot.hasData) {
                    return Text('no data');
                  } else if (snapshot.hasError) {
                    return Text('has err');
                  } else {
                    return Text('not specified');
                  }
                },
                childCount: snapshot.data.docs.length,
              ),
            )
        ],
      );
    },
  );
}

【讨论】:

  • 解决了这个问题,我不得不用Expanded 包裹CustomScrollView
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-03
  • 2021-02-14
  • 1970-01-01
  • 2018-09-25
  • 1970-01-01
  • 2020-04-29
相关资源
最近更新 更多