0 purchases
sliver stream builder
sliver_stream_builder #
Package to transform data stream to sliver list or sliver grid, with helper to create stream from next function
Usage #
Simple usage example #
CustomScrollView(
slivers: [
SliverStreamBuilder<NewsModel>(
stream: NewsNetwork.getNews(),
builder: (ctx, item) => NewsItem(news: item),
)
],
);
copied to clipboard
With custom sliver builder #
CustomScrollView(
slivers: [
SliverStreamBuilder<String>(
stream: ImageNetwork.getImages(),
sliverBuilder: (context, delegate) => SliverGrid(
delegate: delegate,
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 150,
),
),
itemBuilder: (ctx, url) => CachedNetworkImage(imageUrl: url),
),
],
);
copied to clipboard
With empty builder #
CustomScrollView(
slivers: [
SliverStreamBuilder<NewsModel>(
stream: NewsNetwork.getNews(),
builder: (ctx, item) => NewsItem(news: item),
// display when stream done, and not emit any elements
emptyBuilder: (_) => Center(child: Text('Nothing new here :(')),
)
],
);
copied to clipboard
Create stream from next function #
dataStreamWrapper function that manage stream controller and transform next function to stream. Implements pause, resume logic and error processing
// manualy manage state
Stream<NewsModel> getNews() {
int page = 0;
return dataStreamWrapper(() async {
final ans = await dio.get('/news', queryParameters: {'page': page});
final data = (ans.data as List).map((e) => NewsModel.fromJson(e)).toList();
if (data.isEmpty) return null;
// increment page only here for allow to refetch same data if exception was throw
// all errors throw inside this function is captured and added to stream
page += 1;
return data;
});
}
copied to clipboard
dataStreamHelper more safe method to create dataStream cause it prevent some misuse
Stream<NewsModel> getNews() {
return dataStreamHelper.state(0).next((it) async {
final ans = await dio.get('/news', queryParameters: {'page': it.current});
final data = (ans.data as List).map((e) => NewsModel.fromJson(e)).toList();
if (data.isEmpty) return it.done();
return it.next(
data,
it.current + 1, // next state
);
});
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.