简体   繁体   English

避免使用 Future builder 重建屏幕

[英]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.我正在使用FutureBuilderProvider从我的服务器获取数据,这很好,因为它删除了很多样板,但缺点是 FutureBuilder 每次我更改屏幕时都会重建屏幕。

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 ?是否可以缓存未来的构建器或在InitState中使用它?

Is the future builder the right fit for my case?未来的建造者是否适合我的情况?

Future Builder (parts of the code omitted for readability): Future Builder (为了便于阅读,省略了部分代码):

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 ). RefreshIndicator可能是您正在寻找的(文档解释了它是如何工作的比我更好的https://api.flutter.dev/flutter/material/RefreshIndicator-class.ZFC35FDC70D5FC69D235883A8E2C7

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).您可以让您的页面watch您的Provider (可能是ChangeNotifierProvider ,以便它可以notifyListeners并触发重建),并让您的RefreshIndicator.onRefresh调用您的刷新方法来更新包装在您的Provider中的小部件中的值(并在您的异步 function 运行时的页面)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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