reactive_shared_prefs

A Flutter package that provides a reactive wrapper for SharedPreferences.
It allows you to watch keys as streams, so multiple widgets or BLoCs can react to changes automatically.

Features

  • Supports int, double, bool, String, List<String>.
  • Streams emit current value immediately and subsequent changes.
  • Automatic updates across multiple listeners.
  • Easy to integrate with BLoC, Provider, or StreamBuilder.
  • Retrieve all saved keys and values with getAll().
  • Watch multiple keys at once with watchAll().

Installation

Add to your pubspec.yaml:

dependencies:
  reactive_shared_prefs: ^1.0.1+1


import 'package:reactive_shared_prefs/reactive_shared_prefs.dart';

final prefs = ReactiveSharedPreferences();
await prefs.init();

// Listen for changes of a single key
prefs.watch<int>('counter').listen((value) {
  print('Counter: $value');
});

// Watch multiple keys
prefs.watchAll(['counter', 'username']).listen((entry) {
  print('${entry.key} changed → ${entry.value}');
});

// Update value
await prefs.set<int>('counter', 1);

// Remove key
await prefs.remove('counter');

// Clear all
await prefs.clear();

// Get a single value
final counter = prefs.get<int>('counter');

// Get all saved values as a Map<String, dynamic>
final allValues = prefs.getAll();
print(allValues);


Methods

Future<void> init() — initialize SharedPreferences instance.

T? get<T>(String key) — retrieve a value for a key.

Future<void> set<T>(String key, T value) — set a value for a key.

Future<void> remove(String key) — remove a key.

Future<void> clear() — clear all keys.

Stream<T?> watch<T>(String key) — watch a single key as a stream.

Stream<MapEntry<String, dynamic>> watchAll(List<String> keys) — watch multiple keys as streams.

Map<String, dynamic> getAll() — get all saved key-value pairs.

void dispose() — close all internal streams.

Libraries

reactive_shared_prefs
Reactive Shared Preferences