简体   繁体   中英

Avoid rebuilding screen with Future builder

I am using a FutureBuilder and Provider to fetch data from my server, it is fine because it remove a lot of boilerplate, but the downside is that FutureBuilder rebuild the screen everytime I change my screen.

For a perfomance standpoint this is not good, I prefer to let the user use a pull to refresh if needed and change my list of objects if needed.

Is it possible to cached the future builder or use it in a InitState ?

Is the future builder the right fit for my case?

Future Builder (parts of the code omitted for readability):

FutureBuilder(
      future: Provider.of<MyProvider>(context, listen: false).fetch();,
      builder: (ctx, dataSnapshot) {
       ...
            final Data =
                Provider.of<MyProvider>(context, listen: false);
            return Consumer<MyProvider>(
              builder: (ctx, orderData, child) {
                ....
                itemCount: data.length,
                        itemBuilder: (ctx, i) => MyItem()
              },
            );
          }
        }
      },
    ),

RefreshIndicator might be what you're looking for (the docs explain how it works better than I would https://api.flutter.dev/flutter/material/RefreshIndicator-class.html ).

You can have your page watch your Provider (presumably a ChangeNotifierProvider so it can notifyListeners and trigger rebuilds), and have your RefreshIndicator.onRefresh call your refresh method to update the values in the widget wrapped in your Provider (and show a progress spinner on your page while the async function runs).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM