UniState is an adapter package designed to provide an agnostic approach to state management in Flutter, offering a unified interface that seamlessly integrates with various state management systems while maintaining Flutter's native code style.
The primary goal of UniState is to:
- Create an Adapter Layer: Provide a flexible adapter for different state management systems ๐ ๏ธ
- Maintain Code Flexibility: Allow easy switching between state management approaches ๐
- Preserve Flutter's Code Style: Ensure consistency and idiomatic Flutter development ๐ป
Developers often face challenges when:
- Choosing a state management solution โ๏ธ
- Migrating between different state management systems ๐
- Maintaining a consistent code structure ๐
UniState solves these problems by providing a universal adapter that:
- Decouples your application logic from specific state management implementations ๐งฉ
- Provides a consistent interface across different state management approaches ๐ ๏ธ
- Allows for easy experimentation and migration between state management systems ๐
Flutter offers numerous state management solutions, each with its unique code style and philosophy. Developers often face challenges when:
- Choosing a state management approach ๐
- Migrating between different state management systems ๐
- Avoiding tight coupling to a specific state management library ๐
The primary purpose of UniState is to:
- Create an Adapter Layer: Provide a flexible adapter for different state management systems ๐งฉ
- Maintain Code Flexibility: Allow easy switching between state management approaches ๐
- Preserve Flutter's Code Style: Ensure consistency and idiomatic Flutter development ๐ป
- Preserve Your Code: Protect your application's core logic from changes caused by state management system shifts ๐ก๏ธ
UniState is designed to work with multiple state management approaches, including but not limited to:
- BLoC (Business Logic Component) ๐ผ
- Provider ๐ท๏ธ
- Riverpod ๐ฑ
- Cubit ๐ช
- Custom State Management Solutions ๐ ๏ธ
- Agnostic Integration: Seamlessly work with different state management libraries ๐
- Minimal Overhead: Lightweight adapter that doesn't compromise performance โก
- Flutter-Friendly: Maintains the natural flow and style of Flutter development ๐
- Easy Migration: Simplify transitions between state management approaches ๐ฃ๏ธ
The unistate
package provides a structured approach to state management, allowing developers to manage application state efficiently and integrate it seamlessly with the Flutter widget tree ๐ณ.
UniState<T>
: An abstract class defining a contract for managing state, with methods for getting, updating, and resetting the state ๐.UniStateProvider<T, S>
: AStatefulWidget
that manages the lifecycle of aUniState
instance and provides it to the widget tree ๐ณ.- Extensions: Methods like
read
andwatch
onBuildContext
to interact with the state ๐.
The unistate_adapter
package extends the functionality of the unistate
package by integrating with the flutter_adapter
package, providing adapters for bloc and cubit state management ๐.
BlocAdapter<S, E>
: An adapter that listens to aBloc
and updates its value for reactive UI updates ๐.CubitAdapter<T>
: An adapter that listens to aCubit
and updates its value for reactive UI updates ๐.- Extensions: Methods to convert
Bloc
andCubit
instances intoValueListenable
๐.
- Agnostic Integration: Seamlessly work with different state management libraries ๐
- Minimal Overhead: Lightweight adapter that doesn't compromise performance โก
- Flutter-Friendly: Maintains the natural flow and style of Flutter development ๐
- Easy Migration: Simplify transitions between state management approaches ๐ฃ๏ธ
- Decoupled Architecture: Keep your core application logic independent of state management details ๐งฉ
- Future-Proof Development: Easily adapt to new state management trends and technologies ๐ฎ
- Consistent Developer Experience: Maintain a uniform approach to state management across different parts of your application ๐งโ๐ป
// Unified state management interface
abstract class UniState<T> {
T get state;
void updateState(T newState);
void dispose();
}
// Adapter for different state managers
class StateManagerAdapter<T> implements UniState<T> {
// Adapt various state management systems
// (BLoC, Provider, Cubit, etc.)
}
Add the following dependencies to your pubspec.yaml
:
dependencies:
unistate: ^latest_version
unistate_bloc: ^latest_version
Here's a simple counter app demonstrating UniState's flexibility:
// Define your state
class CounterState {
final int count;
CounterState({required this.count});
}
// Create a state manager implementing UniState
class CounterCubit extends Cubit<CounterState> implements UniState<CounterState> {
CounterCubit() : super(CounterState(count: 0));
@override
void performAction(String actionType, [dynamic payload]) {
switch (actionType) {
case 'increment':
emit(CounterState(count: state.count + 1));
break;
case 'decrement':
emit(CounterState(count: state.count - 1));
break;
}
}
@override
void updateState(CounterState newState) {
emit(newState);
}
@override
void resetState() {
emit(CounterState(count: 0));
}
}
// Use in a Flutter widget
class CounterPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final stateManager = UniStateProvider.of<CounterCubit, CounterState>(context);
return Scaffold(
body: Column(
children: [
ValueListenableBuilder(
valueListenable: stateManager.asValueListenable(),
builder: (context, state, child) {
return Text('Count: ${state.count}');
},
),
ElevatedButton(
onPressed: () => stateManager.performAction('increment'),
child: Text('Increment'),
),
],
),
);
}
}
- UniState Interface: Provides a consistent method for state management ๐ ๏ธ
- Flexible Adapters: Works with various state management libraries ๐
- Easy State Manipulation: Use
performAction()
for state changes ๐
-
BLoC [WIP]
-
Cubit [WIP]
-
Provider
-
Riverpod
-
Custom State Management Solutions ๐ ๏ธ
UniState makes it easy to:
- Switch between state management approaches ๐
- Maintain consistent code structure ๐
- Decouple application logic from state management details ๐งฉ
- Ensure you've imported the necessary packages ๐ฆ
- Check that your state manager implements the
UniState
interface ๐ ๏ธ - Use
UniStateProvider
to wrap your widget tree ๐ณ
Contributions are welcome! Please feel free to submit a Pull Request ๐.
This project is licensed under the MIT License.