1

我想做一个商业应用程序。每个人都建议我让整个应用程序是无状态的。为什么我们使用无状态小部件而不是有状态小部件来制作重型软件?

4

2 回答 2

2

没有“无状态应用程序”之类的东西。与图片相比,将某物称为“应用程序”已经确定了它具有状态的事实。

The problem is, you asked someone a question with context and they gave you an explanation in that context. And when you did not understand that explanation, you did not ask them for clarification, but instead decided to come here and ask your question here, without any of the context you had before. We cannot possibly guess that context. I'm sure they were right in the context you had, but with the context gone, the plain statement of fact that is left is simply wrong. Next time, ask for clarification when you do not understand something immediately to preserve the context.

Maybe you meant why you should not use a StatefulWidget as your first Widget in the tree?

You can find an explanation here: runApp throws an exception with stateful widget

Maybe you wanted to know the difference between Stateful and Stateless Widgets?

您可以在这里找到解释:Flutter 中的有状态和无状态小部件之间的关系是什么?

于 2020-12-06T08:36:40.113 回答
2

StatefulWidget需要一个State类。这意味着用户可以查看的一件事是开发人员编写的 2 个类。维护更多的代码就是更多的工作。

每个人都StatefulWidget可以做任何工作。它可以创建 HTTP 客户端并发出网络请求。它可以打开一个文件并读取它。Widget代码不应该做那样的事情。当您挑战自己编写StatelessWidget代码时,您会避免将“业务逻辑”放入Widget代码中。

当您将逻辑放入小部件时,这会让您的生活变得艰难。您必须测试该代码,现在您必须在出现错误时读取每个单独的小部件,或者当您的队友编写具有逻辑的新小部件代码时。

这也意味着一个小部件有多个改变的理由。StatefulWidget如果逻辑不正确,或者用户不喜欢 UI 的外观,A将会改变。而 aStatelessWidget只会改变 UI 的外观。

繁重的软件只有在制作它的部分很轻的情况下才能制作,否则在添加新功能时会卡住。StatelessWidget比 轻StatefulWidget

于 2020-12-06T08:01:03.617 回答