Flutter Programmatically trigger FutureBuilder

Using StreamBuilder is a solution, however, to trigger the FutureBuilder programmatically, just call setState, it'll rebuild the Widget.

return RefreshIndicator(
  onRefresh: () {
          setState(() {});
        },
    ...
)

Why not using a StreamBuilder and a Stream instead of a FutureBuilder?

Something like that...

class _YourWidgetState extends State<YourWidget> {
  StreamController<String> _refreshController;

  ...
  initState() {
    super...
    _refreshController = new StreamController<String>();
   _loadingDeals();
  }

  _loadingDeals() {
    _refreshController.add("");
  }

  _handleRefresh(data) {
    if (x) _refreshController.add("");
  }

  ...
  build(context) {
    ...
    return StreamBuilder(
      stream: _refreshController.stream,
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        return RefreshIndicator(
          onRefresh: _handleRefresh(snapshot.data),
            ...
        )
      }
    );
  }
}

I created a Gist with the Flutter main example using the StreamBuilder, check it out

Tags:

Dart

Flutter