Flutter - 热重载 - 从 State.build 函数外部更改值
出于可管理性的目的,我使用包括文本、尺寸、颜色、路径等在内的值构建了应用程序,所有这些值都放入了除 StatefulWidget 类文件之外的不同文件中。我以前从未寻找过这个问题的解决方案,所以我采用了更困难的方法,在完成小部件的界面后将变量移出。下面举例。
class TextState extends State<TextWidget>{
@override
Widget build(BuildContext context) => Text(Values.text); //Changed from Text('EXAMPLE')
}
--- In different file ---
class Values{
static String text = 'EXAMPLE';
}
但今天我需要从上到下重新设计应用程序。当然,不幸的是,当我更改变量的值时,热重载会忽略它,因为它被认为是状态值,但在我的情况下,它实际上不是运行时状态更新。
是否有任何解决方案,以便我可以更改值并查看结果,而无需每次都进行繁琐的热重启,也不必为了热重载而一半地反转我的代码?先感谢您。
For managability purpose, I've built the app with the values including texts, dimensions, colors, paths, and etc, all been put into different files other than the StatefulWidget
class files. I never looking for the solution for this problem before so I did it the harder way, moving the variables out after finalizing the widget's interface. Below for example.
class TextState extends State<TextWidget>{
@override
Widget build(BuildContext context) => Text(Values.text); //Changed from Text('EXAMPLE')
}
--- In different file ---
class Values{
static String text = 'EXAMPLE';
}
But today I need to redesign the app, top to bottom. Of course unfortunately when I change the variables' value, hot reload ignores it because it's considered to be a state value, except in my case it's actually not a runtime state update.
Is there any solution so I can change the values and see the result without tediously hot restart everytime and also without have to reverse my code half way in order for hot reload to work?. Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是预期的行为。
请参阅此处了解更多信息 详细信息。
This is expected behavior.
See here for more details.
我知道这有点晚了,但尝试通过 getter 函数访问它。我希望这对其他人有帮助。
I know this is a little late but try accessing it through the getter function. I'm hoping this would help someone else.