Flutter - 热重载 - 从 State.build 函数外部更改值

发布于 2025-01-17 04:25:29 字数 560 浏览 0 评论 0原文

出于可管理性的目的,我使用包括文本、尺寸、颜色、路径等在​​内的值构建了应用程序,所有这些值都放入了除 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 技术交流群。

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

发布评论

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

评论(2

旧时浪漫 2025-01-24 04:25:29

这是预期的行为。

在 Dart 中,静态字段是延迟初始化的。这意味着
第一次运行 Flutter 应用程序并读取静态字段时,它会被设置
无论其初始值设定项被评估为什么值。全局变量
静态字段被视为状态,因此不是
在热重载期间重新初始化。

请参阅此处了解更多信息 详细信息

This is expected behavior.

In Dart, static fields are lazily initialized. This means that the
first time you run a Flutter app and a static field is read, it is set
to whatever value its initializer was evaluated to. Global variables
and static fields are treated as state, and are therefore not
reinitialized during hot reload.

See here for more details.

心如荒岛 2025-01-24 04:25:29

我知道这有点晚了,但尝试通过 getter 函数访问它。我希望这对其他人有帮助。

class TextState extends State<TextWidget>{
    @override
    Widget build(BuildContext context) => Text(Values.text); //Changed from Text('EXAMPLE')
}

--- In different file ---


class Values{
   // Returning as a function.
    static String get text { 
     return 'EXAMPLE'
    };
}

I know this is a little late but try accessing it through the getter function. I'm hoping this would help someone else.

class TextState extends State<TextWidget>{
    @override
    Widget build(BuildContext context) => Text(Values.text); //Changed from Text('EXAMPLE')
}

--- In different file ---


class Values{
   // Returning as a function.
    static String get text { 
     return 'EXAMPLE'
    };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文