简体   繁体   English

Flutter 设置状态 Function

[英]Flutter setState Function

I noticed the more you got variables in your statefulWidget, the more the setState function takes time to complete.我注意到你的 statefulWidget 中的变量越多, setState function 完成的时间就越多。 Making the app kinda slow, and this is quite annoying.让应用程序有点慢,这很烦人。

Is there a way to change state for Only one variable , please?请问有没有办法将 state 更改为只有一个变量 I mean something like this:我的意思是这样的:

setState(...varToUpdate)

No. setState by design is rebuilding all widgets that depend on the state on which the method os being called.不,setState 按照设计正在重建所有依赖于调用方法 os 的 state 的小部件。

If you refactor your monolithic widget into sub-widgets, you can have finer-grain control over what gets rebuilt.如果您将整体小部件重构为子小部件,则可以更细粒度地控制要重建的内容。 Also, you should look into a state management solution like RiverPod to be able to narrow down "consumers" to be associated with their triggers, which helps tremendously.此外,您应该研究像 RiverPod 这样的 state 管理解决方案,以便能够缩小与其触发器相关联的“消费者”的范围,这非常有帮助。

Also, if your build is expensive, you are doing something wrong.此外,如果您的构建很昂贵,那么您做错了什么。 A build should be cheap, capable of being performed 60 times per second with no I/O or expensive calculations.构建应该是便宜的,能够在没有 I/O 或昂贵的计算的情况下每秒执行 60 次。

No there isn't, since setState rebuilds the whole widget.不,没有,因为setState重建了整个小部件。 It updates all variables.它更新所有变量。

Performance is just slow in debug mode.在调试模式下性能很慢。 Once you build your app in release mode it should be faster.一旦你在发布模式下构建你的应用程序,它应该会更快。

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

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