【问题标题】:flutter/dart, why a static field is set twice?颤振/飞镖,为什么要设置两次静态字段?
【发布时间】: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;
        }
    );  
 }

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

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

【问题讨论】:

    标签: flutter dart static field


    【解决方案1】:

    我解决了我的问题。

    我从文件头中删除了所有import '...';,然后又一个一个添加。

    我不明白原因。

    【讨论】:

      猜你喜欢
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      • 2020-12-25
      • 2020-11-05
      • 1970-01-01
      • 2023-03-16
      • 2020-05-19
      相关资源
      最近更新 更多