dartflutterfuturebuilder

When should I use a FutureBuilder?


I was wondering when I should use the future builder. For example, if I want to make an http request and show the results in a list view, as soon as you open the view, should I have to use the future builder or just build a ListViewBuilder like:

new ListView.builder(
        itemCount: _features.length,
        itemBuilder: (BuildContext context, int position) {
...stuff here...
}

Moreover, if I don't want to build a list view but some more complex stuff like circular charts, should I have to use the future builder?

Hope it's clear enough!


Solution

  • 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:

    Tasks for FutureBuilder:

    Pros of FutureBuilder

    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.

    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.