[英]Flutter: Mutable fields in stateless widgets
The class StatelessWidget
is marked as immutable
.类
StatelessWidget
被标记为immutable
。 However, I am using the scoped model
, which means that I avoid StatefulWidget
and use the model
to alter state
in StatelessWidget
.不过,我使用的
scoped model
,这意味着我避免StatefulWidget
和使用model
来改变state
的StatelessWidget
。 This leads to me having non-final fields
in StatelessWidget
, which doesn't cause errors
, because it's just a warning
.这导致我在
StatelessWidget
有non-final fields
,这不会导致errors
,因为它只是一个warning
。 But I wondered if there is a better way?但我想知道是否有更好的方法?
Stateless widgets should only have final fields, with no exceptions .无状态小部件应该只有 final 字段,没有例外。 Reason: When the parent widget is rebuilt for some reason (screen rotation, animations, scrolling...), the
build
method of the parent is called, which causes all widgets to be reconstructed.原因:当父widget由于某种原因(屏幕旋转、动画、滚动...)重建时,会调用parent的
build
方法,导致所有widget都被重建。
Classes the extend StatefulWidget
must follow the same rule, because those are also reconstructed.扩展
StatefulWidget
类必须遵循相同的规则,因为它们也是重构的。 Only the State
, which can contain mutable fields, is kept during the lifetime of widget in the layout tree.在布局树中小部件的生命周期中,只有
State
可以包含可变字段。
There is no reason to avoid StatefulWidget
.没有理由避免
StatefulWidget
。 It is a fundamental building block of Flutter.它是 Flutter 的基本构建块。
In fact, ScopedModelDescendant
is also a stateful widget.事实上,
ScopedModelDescendant
也是一个有状态的小部件。 The primary benefit of scoped_model is that you can separate the business logic from the widget layer. scoped_model 的主要好处是您可以将业务逻辑与小部件层分开。 It doesn't eliminate the need for stateful widgets.
它并没有消除对有状态小部件的需求。
Use stateful widgets for:将有状态小部件用于:
ScopedModel
widget).ScopedModel
小部件的小部件)。 Store the Model
instance in the State
.Model
实例存储在State
。TextEditingController
, state of a checkbox)TextEditingController
,复选框状态)AnimationController
sAnimationController
的动画小部件Controller
( TabController
, ScrollController
, ...)Controller
结尾的任何内容( TabController
, ScrollController
,...) It is often a good idea to make the "page" widgets (widgets which build a Scaffold
, accessible using the Navigator
) stateful.使“页面”小部件(构建
Scaffold
小部件,可使用Navigator
访问)有状态通常是一个好主意。 Often these are the hosts for scoped models.通常这些是作用域模型的宿主。
Here's you question:这是你的问题:
Do you think there is a better approach to accomplish what I want while keeping the "short class structure" that helps me keep oversight and being able to trigger rebuilds of those classes from anywhere?
你认为有没有更好的方法来完成我想要的,同时保持“短类结构”来帮助我保持监督并能够从任何地方触发这些类的重建?
What you're asking for here ↑ seems to be one more app state management approach that should be better than scoped model .你在这里要求的 ↑ 似乎是另一种应该比 scoped model 更好的应用程序状态管理方法。
As you know, app state management approaches are a set of techniques that allow you as a developer:如您所知,应用程序状态管理方法是一组技术,可让您作为开发人员:
Binding data with widgets, in turn, helps you as a developer:反过来,将数据与小部件绑定可以帮助您作为开发人员:
Maybe, for that purpose, you can make use of rxdart :也许,为此,您可以使用rxdart :
Here you will find some very helpful list of app state management approaches that can lead you to a better way of app development:在这里,您会找到一些非常有用的应用状态管理方法列表,它们可以引导您获得更好的应用开发方式:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.