如何在CustomScrollWidget中使用StreamBuilder?

时间:2020-08-31 08:38:33

标签: flutter flutter-sliver stream-builder

我正在尝试显示来自sqlite的一些数据,并且一切正常,但是我无法在CustomScrollView中使用StreamBuilder,它说:

RenderViewport预期使用RenderSliv​​er类型的子代,但接收到类型为RenderSliv​​er的子代 RenderPositionedBox。

然后我用SliverPadding包裹了StreamBuilder(通常SliverPadding由StreamBuilder包裹),但是这次它说:

RenderSliv​​erPadding预期为RenderSliv​​er类型的子代,但接收到类型为RenderSliv​​er的子代 RenderPositionedBox。

我尝试使用SliverToBoxAdapter并将其包装在StreamBuilder中,但没有解决我的问题,因此我该如何实现?

这是我的代码的最后状态:

CustomScrollView(
  slivers: <Widget>[
    SliverAppBar(
      centerTitle: true,
      floating: false,
      snap: false,
      pinned: false,
      expandedHeight: 0,
      elevation: 3,
      forceElevated: true,
      backgroundColor: Theme.of(context).scaffoldBackgroundColor,
      brightness: Brightness.light,
      title: Text(
        'Your Lists',
        style: GoogleFonts.openSans(
            fontSize: 22, fontWeight: FontWeight.bold, color: Colors.black),
      ),
    ),
    SliverPadding(
      padding: EdgeInsets.fromLTRB(16, 16, 16, 74),
      sliver: StreamBuilder<List<Category>>(
          stream: bloc.getAll().asStream(),
          builder: (context, AsyncSnapshot<List<Category>> snapshot) {
            if (snapshot.hasData) {
              return SliverGrid(
                delegate: SliverChildBuilderDelegate(
                    (BuildContext context, int index) {
                  if (index == snapshot.data.length) {
                    return ListAddCard();
                  }
                  return ListCard(
                    category: snapshot.data[index],
                  );
                }, childCount: snapshot.data.length + 1),
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2,
                    mainAxisSpacing: 16,
                    crossAxisSpacing: 16,
                    childAspectRatio: 1.1),
              );
            }
            return Center(child: CircularProgressIndicator());
          }),
    )
  ],
);

2 个答案:

答案 0 :(得分:0)

这里的问题是SliverPadding期望一个RenderSliverPadding,但是您给它一个RenderPositionedBox。相反,您可以删除SliverPadding并将其包装在SliverGrid中,而不是StreamBuilder中。
试试这个:

StreamBuilder<List<Category>>(
          stream: bloc.getAll().asStream(),
          builder: (context, AsyncSnapshot<List<Category>> snapshot) {
            if (snapshot.hasData) {
              return  SliverPadding(
      padding: EdgeInsets.fromLTRB(16, 16, 16, 74),
      sliver: SliverGrid(
                delegate: SliverChildBuilderDelegate(
                    (BuildContext context, int index) {
                  if (index == snapshot.data.length) {
                    return ListAddCard();
                  }
                  return ListCard(
                    category: snapshot.data[index],
                  );
                }, childCount: snapshot.data.length + 1),
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2,
                    mainAxisSpacing: 16,
                    crossAxisSpacing: 16,
                    childAspectRatio: 1.1),),
              );
            }
            return Center(child: CircularProgressIndicator());
          }),

答案 1 :(得分:0)

解决方案是,您应该将整个CustomScrollView包装在Streambuilder中,而不是反过来。在使用这种策略之前,我一直面临着同样的问题。