SETSTATE怎么了?

发布于 2025-02-10 04:44:44 字数 774 浏览 1 评论 0原文

Flutter SetState是一种内置的状态管理方法,在flutter官方文档中列出 https://docs.flutter.flutter.dev/deveverment.deveverment/data-data-data-data-data-data-data-data-data-wovernation /data-data-data-data-data-data-wovernation /data-data-data-data-data-data-woverment/data-data-data-woveration- and-backend/state-mgmt/options

为什么有这么多状态管理替代品的flutter setState side riverpodgetxBlocredux ...? setState的糟糕是,它激发了如此多的州管理库的存在。

这就是扑朔迷离本身通过将这些图书馆放在正式文档中来认可这些图书馆的地步。

这一切都归结为仅表现吗?如果是这样,我们是否有实际的性能数据证明SetState非常糟糕?

Flutter setState is a built-in state management approach listed in the Flutter official documentation along with a list of other libraries for state management
https://docs.flutter.dev/development/data-and-backend/state-mgmt/options

Why are there so many state management alternatives to Flutter native setState such RiverPod, GetX, BloC, Redux...? What is so bad with setState that motivates so many state management libraries to come into existence.

This is to the point that Flutter itself is endorsing those libraries by putting them on their official documentation.

Does it all come down to just performance? If so, do we have actual performance data that has proven that setState is very bad?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も星光 2025-02-17 04:44:44
  • 为什么有如此多的州管理替代方案?

    Flutter团队在一开始就没有认可解决方案,社区开始生下我们现在拥有的许多选择


  • 这一切都归结为仅仅是表现吗?

    并非真的,如果不经意使用,您可能会通过SetState或任何其他解决方案获得不良/良好的性能。其他国家管理解决方案确实允许关于何时重建UI或其中的各个部分的更多粒度,但是他们所做的更好的是应用这些优化甚至默认为某些人的方便(例如:Bloc或Riverpod如何不重建) UI如果发射状态相同)。

setState 的许多部分都没有任何错误,而且许多部分都会使用它。我更喜欢它短暂的状态(如Flutter Team所述),而不是应用程序状态,因为它与UI和widget链接了太多的应用程序状态生命周期。

当然,您可以仅使用setState进行整个应用程序,但是其他选项(例如简化的窗口小部件),RiverPod或bloc将UI从“应用程序状态”或业务逻辑中解脱出来。它们使从UI隔离行为并对其进行测试变得更加容易。 简单状态管理的一个很好的例子官方文档,甚至使用骨架模板创建一个新项目flutter创建样本-t骨架,这是Flutter团队的指导,我同意这是非常好的默认值。

  • Why are there so many state management alternatives?

    The Flutter team did not endorsed a solution in the beginning and the community started to roll their own giving birth to the many options we have now


  • Does it all come down to just performance?

    Not really you could get bad/good performance with setState or any other solution if used carelessly. Other state management solutions do permit more granularity regarding when to rebuild the UI or sections of it, but what they do make better is how convenient is to apply those optimizations or even default to some of them (eg: how Bloc or Riverpod do not rebuild the UI if the emitted states are the same).

There is nothing inherently wrong with setState and many parts of flutter use it .I prefer it for ephemeral state (as explained by the flutter team) , not app state as it ties a little bit too much your app state with the UI and widget lifecycle.

You can certainly do a whole app using only setState but other options like provider(which is mostly just Inherited Widgets simplified), riverpod, or bloc decouple the UI from the "App state" or business logic. They make it easier to segregate behavior from UI and test it. A great example of simple state management is in the official docs or even creating a new project using the skeleton template flutter create sample -t skeleton which are guidelines by the flutter team and I agree that are very good defaults.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文