I am trying to use a StreamBuilder
to fetch data and I want to display that data using a SliverList
all inside a CustomScrollView
so I can take advantage of the features that come with the CustomScrollView
.
Any ideas on how I can achieve this?
If you need to build a widget dependent on the result of a Stream, you can utilize the StreamBuilder widget. You can make a Stream and pass it as the stream contention. Then, at that point, you need to pass an AsyncWidgetBuilder work that is utilized to construct a widget dependent on the snapshots of the Stream.
StreamBuilder is a widget that builds itself based on the latest snapshot of interaction with a stream.
A stream can have multiple listeners and all those listeners can get the pipeline. puting in all will get equal value. The way you put values on the stream is by using the Stream Controller. A stream builder is a widget that can convert user-defined objects into a stream.
StreamBuilder<T> class Null safety. Widget that builds itself based on the latest snapshot of interaction with a Stream. Widget rebuilding is scheduled by each interaction, using State. setState, but is otherwise decoupled from the timing of the stream.
With CustomScrollView, you can create various scrolling effects like grids, lists, and expanding headers. One of its properties is slivers, in which you can pass a collection of widgets.
Eg: A ListView and its Sliver equivalent is demonstrated in the code below. The CustomScrollView is a ScrollView type widget that lets you create different scrolling effects using Slivers such as expanding headers, lists, grids etc. Sliver components are widgets that go inside slivers that necessarily produce RenderSliver objects.
Flutter's CustomScrollView is basically a ScrollView with some effects. With CustomScrollView, you can create various scrolling effects like grids, lists, and expanding headers. One of its properties is slivers, in which you can pass a collection of widgets.
Each widget in slivers must have RenderSliver objects. Example of compatible widgets include SliverAppBar, SliverToBoxAdapter, SliverList, and SliverGrid. Below is a basic example how to create a CustomScrollView widget. The SliverAppBar is a material design app bar that's compatible with CustomScrollView.
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(),
);
},
),
);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With