1

我试图在 a和 aStreambuilder中显示检索到的数据时使用 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
4

1 回答 1

3

您应该只将 Sliver 小部件传递给slivers列表。 StreamBuilder不是 Sliver,所以你可以CustomScrollViewStreamBuilder.

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,
              ),
            )
        ],
      );
    },
  );
}
于 2021-05-11T00:40:14.607 回答