UPPER Flutter Stores is a lightweight, powerful, and flexible state management solution designed to simplify your Flutter applications. Inspired by the simplicity of Svelte Stores, it provides a unified store API with specialized independent stores for advanced use cases like persistence, snapshots, undo/redo functionality, middleware, and more.
SharedPreferences
.StoreLifecycleManager
for automatic cleanup of listeners and subscriptions.Add the package to your pubspec.yaml
:
dependencies:
upper_flutter_stores: ^0.1.15
Run:
flutter pub get
import 'package:upper_flutter_stores/upper_flutter_stores.dart';
void main() {
final store = Store<int>(
0, // Initial state
enableDebugging: true, // Enable debug logs
enableUndoRedo: true, // Add undo/redo capability
enableSnapshots: true, // Enable snapshot functionality
enablePersistence: true, // Persist state across app restarts
persistKey: 'counter_store', // Key for persistence
fromJson: (json) => json['value'] as int,
toJson: (state) => {'value': state},
);
// Listen to state changes
store.subscribe((state) {
print('State updated: $state');
});
// Perform state updates
store.set(10); // Update state
store.undo(); // Undo last change
store.takeSnapshot(); // Take a snapshot
store.persist(); // Save state
}
Check the Example for more details.
final asyncStore = AsyncStore<String>(enableDebugging: true);
asyncStore.run(() async {
await Future.delayed(Duration(seconds: 2));
return "Data Loaded!";
});
asyncStore.subscribe((state) {
if (state.isLoading) {
print("Loading...");
} else if (state.data != null) {
print("Data: ${state.data}");
} else if (state.error != null) {
print("Error: ${state.error}");
}
});
final undoableStore = UndoableStore<int>(0, enableDebugging: true);
undoableStore.set(10);
undoableStore.set(20);
undoableStore.undo(); // Reverts to 10
undoableStore.redo(); // Returns to 20
final middlewareStore = MiddlewareStore<int>(0, enableDebugging: true);
// Add a middleware to log state transitions
middlewareStore.addMiddleware((oldState, newState) {
print("State changed from $oldState to $newState");
});
// Add another middleware for validation
middlewareStore.addMiddleware((oldState, newState) {
if (newState < 0) {
print("Warning: State is negative");
}
});
// Update state
middlewareStore.set(10); // Logs: State changed from 0 to 10
middlewareStore.set(-5); // Logs: State changed from 10 to -5 and Warning: State is negative
StoreLifecycleManager
Wrap your app with StoreLifecycleManager
to automatically manage listeners:
StoreLifecycleManager.register(context, () {
print("Dispose callback executed");
});
SharedPreferences
.Enable debugging with enableDebugging: true
in any store. It provides:
This project is licensed under the MIT License. See the LICENSE file for details.
See the CHANGELOG.md for the list of updates and improvements.
Weβd love your feedback! Feel free to open issues or contribute to the repository.
This package comes with extensive unit tests to ensure reliability. Run tests with:
flutter test
This checklist outlines planned features for the upper_flutter_stores package to enhance functionality, usability, and appeal to both beginner and advanced users.
Multi-Store Sync
Caching Support
DevTools Event Integration
dart:developer
timeline.Custom Event Viewer
upper_flutter_stores.*
events.Enhanced Debugging Panel
Type Safety Enhancements
TypedStore
examples for common use cases like models, lists, and maps.Error Boundary Middleware
Immutable State Helpers
Lazy Store Initialization
State Export and Import
CLI Integration
Enhanced Documentation
Community Marketplace
Batch State Updates
Selective Store Listeners
--
Check the full documentation for more details and advanced examples.
If youβre interested in contributing or suggesting additional features, please refer to the CONTRIBUTING.md.