KenAragorn/create_flutter_provider_app

SharedPreferences / not loading on app initialization

Closed this issue · 1 comments

The issue is when I close and reopen my app, the color theme and language is not set according to my settings, it's always the default.

In lib/my_app.dart lines 40

locale: languageProviderRef.appLocale,

and 73

themeMode: themeProviderRef.isDarkModeOn

are the issue.

As these are async methods, from lib/providers/language_provider.dart and lib/providers/theme_provider.dart respectively, when the app initially runs, the information isn't there yet.

Let me know if anyone has thoughts how to fix it.

I resolved this by surrounding the AuthWidgetBuilder widget in the lib/my_app.dart file with

FutureBuilder(
  future: SharedPreferenceHelper.initSharedPrefs(),
  builder: (_, __) => AuthWidgetBuilder(

And adding a static method (below) to the lib/caches/sharedpref/shared_preference_helper.dart

static Future<void> initSharedPrefs() async {
  await SharedPreferences.getInstance();
}

This way, the app start waits for the shared prefs module to be loaded.