Search code examples
firebaseflutterdartfirebase-remote-config

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


I am trying to call a remoteConfig string inside my List

    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(''),
      },
    ];
}

However, it shows me the error -

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

Solution

  • Move the initialization of taskList to initState().

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