Shard is a lightweight, powerful state management solution for Flutter applications. Built on Flutter's ChangeNotifier, Shard provides a clean and intuitive API for managing application state with minimal boilerplate.
Shard follows Flutter's reactive programming model. Create a shard, emit state changes, and widgets automatically rebuild when state changes.
class CounterShard extends Shard<int> {
CounterShard() : super(0);
void increment() {
emit(state + 1);
}
}
Automatically save and restore state with PersistentShard. Choose your preferred local storage solution (SharedPreferences, Hive, Isar, etc.) - no need for separate persistence libraries or complex setup.
class TodoShard extends PersistentShard<TodoState> {
@override
String get persistenceKey => 'todos';
TodoShard() : super(
const TodoState(todos: []),
storageFactory: () => SharedPreferencesStorage.create(), // Or HiveStorage, IsarStorage, etc.
serializer: TodoSerializer(),
);
}
Built-in debounce and throttle mixins help prevent excessive state updates and API calls.
void updateSearchQuery(String query) {
debounce('search', () {
emit(state.copyWith(searchQuery: query));
}, duration: const Duration(milliseconds: 500));
}
Widgets like ShardProvider, ShardBuilder, and ShardSelector make it easy to integrate with Flutter's widget tree. Context extensions provide convenient access.
// Using ShardBuilder
ShardBuilder<CounterShard, int>(
builder: (context, count) {
return Text('Count: $count');
},
)
// Using context extensions
context.read<CounterShard>().increment();
Ready to get started? Check out the Installation Guide to add Shard to your project.