FutureBuilder removes boilerplate code.
Let’s say you want to fetch some data from the backend on page launch and show a loader until data comes.
Tasks for ListBuilder:
- Have two state variables,
dataFromBackendandisLoadingFlag - On launch, set
isLoadingFlag = true, and based on this, showloader. - Once data arrives, set data with what you get from backend and set
isLoadingFlag = false(insidesetStateobviously) - We need to have a
if-elsein widget creation. IfisLoadingFlagistrue, show theloaderelse show thedata. On failure, show error message.
Tasks for FutureBuilder:
- Give the async task in
futureof Future Builder - Based on
connectionState, show message (loading,active(streams),done) - Based on
data(snapshot.hasError), show view
Pros of FutureBuilder
- Does not use the two state variables and
setState - Reactive programming (
FutureBuilderwill take care of updating the view on data arrival)
Example:
FutureBuilder<String>(
future: _fetchNetworkCall, // async work
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting: return Text('Loading....');
default:
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
else
return Text('Result: ${snapshot.data}');
}
},
)
Performance impact:
I just looked into the FutureBuilder code to understand the performance impact of using this.
- FutureBuilder is just a
StatefulWidgetwhosestatevariable is_snapshot - Initial state is
_snapshot = AsyncSnapshot<T>.withData(ConnectionState.none, widget.initialData); - It is subscribing to
futurewhich we send via the constructor and update thestatebased on that.
Example:
widget.future.then<void>((T data) {
if (_activeCallbackIdentity == callbackIdentity) {
setState(() {
_snapshot = AsyncSnapshot<T>.withData(ConnectionState.done, data);
});
}
}, onError: (Object error) {
if (_activeCallbackIdentity == callbackIdentity) {
setState(() {
_snapshot = AsyncSnapshot<T>.withError(ConnectionState.done, error);
});
}
});
So the FutureBuilder is a wrapper/boilerplate of what we do typically, hence there should not be any performance impact.