flutter - 颤振/飞镖,为什么要设置两次静态字段?
问题描述
我的代码是:
class Constants {
Constants._();
static IrisLocaleDelegate localeDelegate = _prepare();
static IrisLocaleDelegate _prepare() {
print('prepare.....'); <<-- this line print twice
return IrisLocaleDelegate();
}
}
class Initial {
Initial._();
static Future<bool> init() async {
/// when this line is call, [prepare.....] is print for 2th time. why?
MyLocalization.setDelegate(Constants.localeDelegate);
...
}
}
main(){
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// it is simpled
return MaterialApp(
...
builder: (context, home) {
Initial.init(); <<- call init
return home;
}
);
}
为什么?静态字段不应该创建一次吗?
最有趣的是,我在前两个应用程序中使用了这些,没有任何问题。
解决方案
我解决了我的问题。
我import '...';
从文件头中删除了所有内容,然后再次添加。
我不明白原因。
推荐阅读
- r - Tossing 3 fair coins in R
- hyperlink - TestCafe:单击 SSL 链接不会加载页面
- r - 替换文件名的第一个字符
- python - 对“main”的未定义引用找不到条目符号_start
- wordpress - 使用管道联系表格 7 可选收件人 - 包括 CC 和 BCC
- webpack - 什么是 chunk-vendors.js 文件,它是如何创建的?(网络包)
- animation - Flutter:检测到滚动时隐藏和显示应用栏
- node.js - 如何使用 Node.JS 从 GCS 存储桶中读取删除的过期时间?
- html - HTML4 到 HTML5:相当于什么
到 HTML5? - javascript - React-sound 不播放项目文件夹中的 mp3 文件