简体   繁体   English

Flutter:当我可以使用全局变量并改用 setState 时,需要 state 管理库吗?

[英]Flutter: What is the need of state management libraries when I can use global variables and use setState instead?

I understand that bloc pattern or getX would provide extra features other than just state management in flutter, but my main concern is why should I use bloc or getX or any other state management library instead of just using setState with global variables?我知道在 flutter 中,除了 state 管理之外,bloc 模式或 getX 会提供额外的功能,但我主要担心的是为什么我应该使用 bloc 或 getX 或任何其他 state 管理库而不是只使用带全局变量的 setState 管理库?

I do also understand that if I update the value of a global variable from a child class and navigate back to the parent then it's state will not be updated but to overcome this issue, I just called the setState() of parent from the child class.我也明白,如果我从子 class 更新全局变量的值并导航回父级,那么它的 state 将不会更新,但为了克服这个问题,我只是从子级 ZA2F2A2ED4F8EBC1AB61DZC24 调用父级的 setState() . Is there anything wrong with my approach and if yes then what?我的方法有什么问题吗?如果是,那又是什么?

Whenever you calling setState() function, your entire Stateful or Stateless Widget will rebuild again.每当您调用setState() function 时,您的整个 Stateful 或 Stateless Widget 将再次重建。 It makes your application's performace to low level.它使您的应用程序的性能降低。

For Eg: You have a Stateless Widget like Following例如:你有一个无状态的小部件,比如关注

Scaffold(
 body: Column(
  children: [
   Container(
    child: Text(SomeText);
   ),
   FlatButton(
    onPressed: (){
     seState({
      //change Text Function
     });
    }
   )
  ]
 )
)

Here, when you click button and its function is to change value of SomeText ,Now these all code will rebuild and yes, its replace SomeText with new value.在这里,当您单击按钮时,它的 function 是更改SomeText的值,现在这些所有代码都将重建,是的,它将SomeText替换为新值。 But it is not just changed it but it rebuild entire widget to change it.但它不仅仅是改变了它,而是重建了整个小部件来改变它。

Here Instead of setState() method, if you are using any state management libraries, then it will only change value of SomeText without affecting other widgets.这里不是setState()方法,如果您使用任何 state 管理库,那么它只会更改 SomeText 的值而不影响其他小部件。

as a beginer You can try Provider作为初学者你可以试试Provider

setState function call builds the entire widget again which is a performance issue. setState function 调用再次构建整个小部件,这是一个性能问题。 State management manages the state without building the entire widget. State 管理管理 state 而不构建整个小部件。 Also, If your data are used on multiple screen you have to pass that data around using arguments which is cumbersome.此外,如果您的数据在多个屏幕上使用,您必须使用 arguments 来传递该数据,这很麻烦。 That's why we use state management libraries.这就是我们使用 state 管理库的原因。 Hope it answers the question.希望它能回答这个问题。

I'm not very well versed with the performance of flutter, my concerns are more on maintainability:我对 flutter 的性能不是很熟悉,我更关心的是可维护性:

  1. When a child is aware of the parent and vice versa there's too much happening on widgets and you'll fail to layer the abstraction and over the period of time become hard to reason about.当孩子知道父母,反之亦然时,小部件上发生了太多事情,您将无法对抽象进行分层,并且随着时间的推移变得难以推理。

  2. In javascript, we follow the flux pattern which talks about top-down state flow: If your app state doesn't follow unidirectional flow it becomes cumbersome to understand in very little time.在 javascript 中,我们遵循有关自上而下的 state 流的通量模式:如果您的应用程序 state 不遵循单向流,那么在很短的时间内理解起来就会变得很麻烦。

  3. If the state logic is not lifted above, your components won't have a single source of truth if the same data needs to be accessed elsewhere.如果上面没有提到 state 逻辑,那么如果需要在其他地方访问相同的数据,您的组件将没有single source of truth

Hope this makes sense.希望这是有道理的。

The problem with calling setState() is that, it will rebuild all the children, where u call it.调用setState()的问题在于,它将重建所有孩子,你调用它的地方。

You can optimize it, with InheritedWidget .您可以使用InheritedWidget对其进行优化。 But, InheritedWidget gives you a boilerplate code.但是, InheritedWidget为您提供了样板代码。

So, it's always practical to use some state management library rather than maintaining InheritedWidgets .因此,使用一些 state 管理库而不是维护InheritedWidgets总是很实用的。

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

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