简体   繁体   English

Flutter Provider 重建不必要的小部件

[英]Flutter Provider rebuilds unnecessary widgets

I am building a mobile app with provider architecture on flutter.我正在构建一个带有 flutter 提供程序架构的移动应用程序。

As per the design, I am using a model with changeNotifier which has a list of objects in it.根据设计,我使用了一个带有 changeNotifier 的模型,其中包含一个对象列表。

Whenever there is a change in one of the list object the whole list is getting rebuilt which not supposed to be.每当列表对象之一发生变化时,整个列表都会被重建,这不应该是。

To simulate this, I have created a simple app like below.为了模拟这一点,我创建了一个如下所示的简单应用程序。 The top line is a list of items, whereas the bottom one is the detailed widget.顶行是项目列表,而底行是详细的小部件。 But whenever I tap both the list widget and the detailed widget is getting rebuilt.但是每当我点击列表小部件和详细小部件时,都会重建。

Here is the source code.这是源代码。 Github Link Github 链接

Here is the sample screenshot这是示例屏幕截图图片截图

Here are my questions.这是我的问题。

  1. How to listen only to the child object?如何只听子对象?
  2. What is the mistake I have made here?我在这里犯了什么错误?

The provider state management package offerers consumers.提供者状态管理包提供者消费者。 When using consumers you have the option to pass in in a child widget which you do don't wish to rebuild.使用消费者时,您可以选择传入您不希望重建的子小部件。 And a builder methods which then accepts the child(which is mostly in this state static and doesn't need rebuilding) together with the widget which are to be rebuilt in your case the bottom detail card if I'm not mistaking.并且一个构建器方法然后接受子项(它主要处于这种状态静态并且不需要重建)以及将在您的情况下重建的小部件,如果我没有记错的话,底部细节卡。 You can lesrn more from the docs provided they're very self explanatory.您可以从文档中了解更多信息,前提是它们非常易于解释。 https://github.com/rrousselGit/provider/blob/master/README.md https://github.com/rrousselGit/provider/blob/master/README.md

Also the medium post in this link explains a lot on how the provider package works.此链接中的媒体帖子也解释了很多关于提供程序包如何工作的内容。 https://medium.com/flutter-nyc/a-closer-look-at-the-provider-package-993922d3a5a5 https://medium.com/flutter-nyc/a-closer-look-at-the-provider-package-993922d3a5a5

老实说,我认为您不需要担心完全重建,但如果它困扰您,有一个 Selector 小部件,您可以使用它来收听列表中项目的特定值。

Maybe it is a bit late, But for anyone has this problem.也许有点晚了,但是对于任何人来说都有这个问题。 Provider is just a dependency injection framework, You can not use it for state management (alone). Provider只是一个依赖注入框架,您不能将其用于状态管理(单独使用)。 You should've provided us with a sample of your code, but I will assume that You're using Provider with ChangeNotifier .您应该已经向我们提供了您的代码示例,但我假设您正在使用ProviderChangeNotifier In this situation You just have to use the Consumer Widget that have a build function inside this function You put your that is gonna be rebuilt.. Here is a Medium article在这种情况下,您只需要使用在此函数中具有构建功能的Consumer Widget 您将要重建的内容放好.. 这是一篇 Medium 文章

Another way is to use a combination of Provider and Streams and you can use StreamBuilder Widget the same way you use Consumer ..另一种方法是使用ProviderStreams的组合,您可以使用Consumer一样使用StreamBuilder Widget ..

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

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