首页 > 解决方案 > 颤振/飞镖,为什么要设置两次静态字段?

问题描述

我的代码是:

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;
        }
    );  
 }

为什么?静态字段不应该创建一次吗?

最有趣的是,我在前两个应用程序中使用了这些,没有任何问题。

标签: flutterdartstaticfield

解决方案


我解决了我的问题。

import '...';从文件头中删除了所有内容,然后再次添加。

我不明白原因。


推荐阅读