A custom version of Flutter async widgets, and a counter app using the BLoC pattern.

Francesco Mineo
2 min readJan 1, 2019

UPDATE (29–01–19): the code here is outdated, both the FuturedWidget and StreamedWidget are now part of the package Frideos-flutter.

I’m using a lot the FutureBuilder and StreamBuilder widgets, but I find there is a little redundancy (and prone to errors) on writing always something like this:

StreamBuilder(stream: bloc.stream,
builder: (context, AsyncSnapshot snapshot) {
return snapshot.hasData
? // Widget to show
: Container();
}

or this:

FutureBuilder(future: future,
builder: (context, AsyncSnapshot snapshot) {
return snapshot.hasData
? // Widget to show
: CircularProgressIndicator();
}

I tried to make this a little bit easier by creating a custom version of these two widgets:

FuturedWidget

FuturedWidget<String>(future: future, builder: (BuildContext context, AsyncSnapshot<String> snasphot) => Text(snasphot.data),
onWaitingChild: // Widget to show on waiting
onWaiting: () => // or Callback
errorChild: // Widget to show on error
onError: (error) => // or Callback
)

StreamedWidget

StreamedWidget<String>(stream: stream, builder: (BuildContext context, AsyncSnapshot<String> snasphot) => Text(snasphot.data),
noDataChild: // Widget to show when the stream has no data
onNoData: () => // or Callback
errorChild: // Widget to show on error
onError: (error) => // or Callback
)

N.B. The callbacks are executed only if the respective child is not provided.

Example app

I made a simple application using the BLoC pattern and these widgets. You can find the sources on GitHub.

Screenshots:

--

--

Francesco Mineo

Medical doctor, singer & music producer , software developer.