使用 Flutter Provider 就意味着没有 StatefulWidgets? [英] Using Flutter Provider means no StatefulWidgets?

查看:78
本文介绍了使用 Flutter Provider 就意味着没有 StatefulWidgets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正准备用 Flutter 和 Provider 编写我的第一个重要应用程序.我已经阅读了 Provider 如何促进不可变小部件(StatelessWidgets).我的问题是,在使用 Provider 时使用 StatefulWidgets 是否总是反模式?如果不是,那么在 Provider 应用中最好使用 StatefulWidgets 的例子有哪些?

I'm getting ready to write my first nontrivial app with Flutter and Provider. I've read up on how Provider facilitates immutable widgets (StatelessWidgets). My question is, is it always an antipattern to use StatefulWidgets when using Provider? If not, what are examples of when it better to use StatefulWidgets in a Provider app?

编辑

使用 Provider 已经有几个月了,在任何情况下,我仍然喜欢它而不是 StatefulWidgets.我时不时地引入一个StatefulWidget,主要是为了尝试熟悉它们,几乎立即后悔并重构为Provider.前几天我遇到了不刷新的小部件,因为它们是相同的类型,所以正在考虑引入键以便它们刷新.前几次尝试失败了,所以我重构为 Provider 并且一切正常(不需要密钥).

It's been a couple months using Provider and I'm still favoring it over StatefulWidgets in every case. Every now and again I introduce a StatefulWidget, mostly to try to gain familiarity with them, and almost immediately regret it and refactor to Provider. The other day I ran into widgets not refreshing because they were identical types, so was looking at introducing keys so they would refresh. First couple attempts failed, so I refactored into Provider and everything just worked (without the need for keys).

反模式在我的 OP 中不是正确的术语.我想我的问题是,是否有 StatefulWidgets 更干净或更容易/更好用的例子?

Antipattern was not the proper term in my OP. I guess my question is, are there examples where StatefulWidgets are cleaner or otherwise easier/better to use?

推荐答案

provider 不关心你是写无状态/有状态还是其他任何东西(钩子?).

provider doesn't care whether you write stateless/stateful or anything else (hooks?).

它消除了在许多情况下编写 StatefulWidget 的需要,但它并不声称您应该只使用 StatelessWidget.

It removes the need to write a StatefulWidget in many situations, but it doesn't claim that you should use StatelessWidget only.

最后,决定是否需要 StatefulWidget 是您的工作.例如,您在编写动画时可能需要它.

In the end, it's your job to decide if you need a StatefulWidget or not. You may need it when writing animations for example.

这篇关于使用 Flutter Provider 就意味着没有 StatefulWidgets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆