A simple package providing glue methods between notified_preferences and flutter_riverpod.
Use it like any other provider. Make sure you provide an instance of SharedPreferences on startup of your app.
final preferencesProvider =
Provider<SharedPreferences>((_) => throw UnimplementedError());
final hasSeenTutorialProvider =
createSettingProvider(key: 'hasSeenTutorial', initialValue: false);
void main() {
final preferences = await SharedPreferences.getInstance();
runApp(
ProviderScope(
overrides: [
preferencesProvider.overrideWith((_) => preferences),
],
child: const App(),
),
);
}
class App extends ConsumerWidget {
Widget build(BuildContext context, WidgetRef ref) {
final hasSeenTutorial = ref.watch(hasSeenTutorialProvider).value;
if (!hasSeenTutorial) return Text("Hello!");
return OutlinedButton(
child: Text("Exit tutorial"),
onPressed: () => ref.read(hasSeenTutorialProvider).value = true,
);
}
}
For more guidance, check out notified_preference's page.