Skip to main content

reactive_shared_prefs 1.0.1 copy "reactive_shared_prefs: ^1.0.1" to clipboard
reactive_shared_prefs: ^1.0.1 copied to clipboard

Reactive wrapper around SharedPreferences with streams and auto updates.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:reactive_shared_prefs/reactive_shared_prefs.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final prefs = ReactiveSharedPreferences();
  await prefs.init();

  runApp(MyApp(prefs: prefs));
}

class MyApp extends StatelessWidget {
  final ReactiveSharedPreferences prefs;

  MyApp({required this.prefs});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: CounterPage(prefs: prefs));
  }
}

class CounterPage extends StatefulWidget {
  final ReactiveSharedPreferences prefs;

  CounterPage({required this.prefs});

  @override
  _CounterPageState createState() => _CounterPageState();
}

class _CounterPageState extends State<CounterPage> {
  int _counter = 0;

  @override
  void initState() {
    super.initState();
    widget.prefs.watch<int>('counter').listen((value) {
      setState(() {
        _counter = value ?? 0;
      });
    });
  }

  void _increment() async {
    await widget.prefs.set<int>('counter', _counter + 1);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('ReactiveSharedPrefs')),
      body: Center(child: Text('Counter: $_counter', style: TextStyle(fontSize: 24))),
      floatingActionButton: FloatingActionButton(onPressed: _increment, child: Icon(Icons.add)),
    );
  }

  @override
  void dispose() {
    widget.prefs.dispose();
    super.dispose();
  }
}
1
likes
0
points
21
downloads

Publisher

unverified uploader

Weekly Downloads

Reactive wrapper around SharedPreferences with streams and auto updates.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, shared_preferences

More

Packages that depend on reactive_shared_prefs