【问题标题】:Flutter - The instance member 'remoteConfig' can't be accessed in an initializerFlutter - 无法在初始化程序中访问实例成员“remoteConfig”
【发布时间】:2023-03-11 09:57:01
【问题描述】:

我试图在我的List 中调用remoteConfig 字符串

    class HomeModel {
      final RemoteConfig remoteConfig; //HomeModel() is called in Home() and fetches the param from there
      HomeModel({this.remoteConfig});
    
    List taskList = [
      { 
        'title': 'Hello',
        'url': remoteConfig.getString(''),
      },
    ];
}

但是,它向我显示了错误-

The instance member 'remoteConfig' can't be accessed in an initializer

【问题讨论】:

    标签: firebase flutter dart firebase-remote-config


    【解决方案1】:

    taskList的初始化移动到initState()

    List taskList;
    
    @override
    void initState() {
        taskList = [
          { 
            'title': 'Hello',
            'url': remoteConfig.getString(''),
          },
        ];
        super.initState();
    } 
    

    【讨论】:

    • 你能解释一下为什么我们需要在 initState 方法内部而不是在外部进行初始化吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    相关资源
    最近更新 更多