【发布时间】:2021-09-06 07:40:41
【问题描述】:
我的代码是:
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;
}
);
}
为什么?静态字段不应该创建一次吗?
最有趣的是,我在前两个应用程序中使用这些并没有改变,我没有任何问题。
【问题讨论】: