Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Latest commit

 

History

History
43 lines (32 loc) · 1.31 KB

README.md

File metadata and controls

43 lines (32 loc) · 1.31 KB

notified_preferences_riverpod

A simple package providing glue methods between notified_preferences and flutter_riverpod.

Usage

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.