Using a StreamBuilder and a SliverLists In CustomScrollView

In this case it is fine to rerender the whole CustomScrollView. However if you want to rerender just one Sliver in a CustomScrollView, do it like this:

CustomScrollView(
    slivers: <Widget>[
      StreamBuilder(
        stream: stream,
        builder: (ctx, snapshot) {
           return SliverToBoxAdapter(
             child: Text('sliver box'),
           );
        },
      )
    ],
  ),

Remember to always return a Sliver inside the StreamBuilder.


Sure, it's easy, here you have a code sample:

    class SampleStreamBuilder extends StatelessWidget {
      Stream<List<String>> loadData() async* {
        await Future.delayed(Duration(seconds: 3));
        yield List.generate(10, (index) => "Index $index");
      }

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: StreamBuilder<List<String>>(
            stream: loadData(),
            builder: (context, snapshot) {
              return snapshot.hasData
                  ? CustomScrollView(
                      slivers: [
                        SliverList(
                          delegate: SliverChildBuilderDelegate((context, index) {
                            return ListTile(
                              title: Text(snapshot.data[index]),
                            );
                          }, childCount: snapshot.data.length),
                        )
                      ],
                    )
                  : Center(
                      child: CircularProgressIndicator(),
                    );
            },
          ),
        );
      }
    }

Tags:

Dart

Flutter