Replies: 1 comment
-
Just answered this at the bottom of #1597 (comment). Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
final prefs = await SharedPreferences.getInstance();
runApp(
ProviderScope(
overrides: [
sharedPreferencesProvider.overrideWithValue(prefs),
],
child: const MyApp(),
),
);
} I use a function final sharedPreferencesProvider = MustOverrideProvider<SharedPreferences>();
/// ignore: non_constant_identifier_names
Provider<T> MustOverrideProvider<T>() {
return Provider<T>(
(_) => throw ProviderNotOverriddenException(),
);
}
class ProviderNotOverriddenException implements Exception {
@override
String toString() {
return 'The value for this provider must be set by an override on ProviderScope';
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It didn't seem immediately obvious how to create a provider for SharedPreferences. FutureProvider for example, seems like overkill and causes boilerplate in the UI code when yet the fetching of the SharedPreferences.instance is a one-time affair and nearly instant.
This could be included as a simple example for the todo example app, thereby answering that and similar questions for the future.
Beta Was this translation helpful? Give feedback.
All reactions