Home > OS >  Error: LateInitializationError: Field 'sharedPreferences' has not been initialized. Flutte
Error: LateInitializationError: Field 'sharedPreferences' has not been initialized. Flutte

Time:02-13

Error Initializing an already late variable.

In my app there is a composition_root.dart file that composes all the pages. I am migrating code to Flutter 2.10 with a verygoodcli app using verygoodanalysis.

Here is the Composition root:


class CompositionRoot {
  static late SharedPreferences _sharedPreferences;
  static late ILocalStore _localStore;
  static late String _baseUrl;
  static late Client _client;

  static void configure() {
    _localStore = LocalStore(_sharedPreferences);
    _client = Client();
    _baseUrl = 'http://localhost:3000';
  }

  static Widget composeAuthUI() {
    final IAuthApi _api = AuthApi(_baseUrl, _client);
    final _manager = AuthManager(_api);
    final _authCubit = AuthstateCubit(_localStore);
    final _signUpService = SignUpService(_api);

    return BlocProvider(
      create: (BuildContext context) => _authCubit,
      child: AuthPage(_manager, _signUpService),
    );
  }
}

However when I run the code, I seem to get the following error message:


Error: LateInitializationError: Field 'sharedPreferences' has not been
initialized.
    at Object.throw_ [as throw] (http://localhost:40837/dart_sdk.js:5067:11)
    at Function.get sharedPreferences [as sharedPreferences]
    (http://localhost:40837/packages/verygoodapp/composition_root.dart.lib.js:58
    :37)
    at Function.configure
    (http://localhost:40837/packages/verygoodapp/composition_root.dart.lib.js:88
    :118)
    at main
    (http://localhost:40837/packages/verygoodapp/main_development.dart.lib.js:45
    :38)
    at main (http://localhost:40837/web_entrypoint.dart.lib.js:36:29)
    at main.next (<anonymous>)

How can I debug this errror also do you mind explaining whats going on, I can't seem to understand. The app is configured in the main_*.dart file before the UI is composed.

main_*.dart


void main() {
  CompositionRoot.configure();
  bootstrap(() => const App());
}

Our App Widget Class:

class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        appBarTheme: const AppBarTheme(color: Color(0xFF13B9FF)),
        colorScheme: ColorScheme.fromSwatch(
          accentColor: const Color.fromARGB(255, 190, 27, 27),
        ),
      ),
      debugShowCheckedModeBanner: false,
      localizationsDelegates: const [
        AppLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
      ],
      supportedLocales: AppLocalizations.supportedLocales,
      home: CompositionRoot.composeAuthUI(),
    );
  }
}

I can't seem to understand please help. Thanks in advance.

CodePudding user response:

_sharedPreferences has not been initialized. Meaning it is storing none and you are trying to use it

Try this

_sharedPreferences = await SharedPreferences.getInstance();
_localStore = LocalStore(_sharedPreferences);

Then you are good to go

CodePudding user response:

Just as takudzw-m, answered I was able to instantiate the sharedPreferences in this manner.


  /// This is an async method that instantiates
  /// and configures the static variables and
  /// should not but returns void.
  // ignore: avoid_void_async
  static void configure() async {
    _sharedPreferences = await SharedPreferences.getInstance();
    _localStore = LocalStore(_sharedPreferences);
    _client = Client();
    _baseUrl = 'http://localhost:3000';
  }

Thank you StackOverflow Community.

  • Related