flutter_dva_hooks
Flutter Dva Hooks β A hook library built on top of flutter_hooks for Flutter Dva state management. It allows you to use Dva state management in Flutter in a declarative, functional way.
π¦ Installation
Add flutter_dva_hooks to your pubspec.yaml:
dependencies:
flutter_dva_hooks: ^1.0.8
Or install via command:
flutter pub add flutter_dva_hooks
β¨ Features
| Hook | Description |
|---|---|
useDvaConnect |
Listens to Store state changes and automatically refreshes the widget, while providing access to both Props and Store |
useDvaProps |
Retrieves Props only, without listening to Store changes |
useDvaStore |
Retrieves the Store instance only, without listening to Store changes |
1. useDvaConnect β Full connection with auto-refresh
Automatically subscribes to Store state changes and triggers widget rebuild when specified state changes. Supports namespace filtering, custom state mapping, and custom update strategies.
final connect = useDvaConnect(
namespace: 'counter', // Listen to specific namespace
listenKeys: ['count'], // Only listen to specific keys
mapState: (rootState) { // Custom state mapping
return {
'counter': rootState['counter'],
};
},
shouldUpdate: (lState, nState) { // Custom update strategy
return lState.updateAt != nState.updateAt;
},
didMounted: () { // Callback after mount
print('Component mounted');
},
);
// Access props
connect.props?.dispatch(Type('increment'));
connect.props?.history;
// Access store
connect.store?.subscribe((lState, nState) {});
connect.store?.rootState;
// Access mapped state
connect.state?['counter'];
2. useDvaProps β Props only
Use this when you only need Props (dispatch, history, etc.) without subscribing to state changes.
final props = useDvaProps(namespace: 'counter');
// Dispatch actions
props.dispatch(Type('increment'));
// Navigation
props.history;
3. useDvaStore β Store only
Use this when you only need the Store instance without subscribing to changes.
final store = useDvaStore();
// Access root state
store.rootState;
// Subscribe to state changes
store.subscribe((lState, nState) {
// Handle state change
});
π API Reference
useDvaConnect Parameters
| Parameter | Type | Description |
|---|---|---|
namespace |
String? |
Namespace to listen to |
listenKeys |
List<String>? |
Only listen to specific state keys under the namespace |
mapState |
MapState? |
Custom state mapping function from rootState |
shouldUpdate |
ShouldUpdate? |
Custom update decision function |
didMounted |
VoidCallback? |
Callback after first mount and state initialization |
Return Types
- DvaConnect: Contains
props,state, andstore - Props: Contains
dispatch,history, and namespace-related properties - Store: The Store instance from
flutter_dva
π Dependencies
- flutter_hooks β Flutter Hooks implementation
- flutter_dva β Dva state management for Flutter
π License
This project is licensed under the MIT License.
π Links
Libraries
- connect
- Provides the useDvaConnect hook for connecting widgets to the Dva Store.
- flutter_dva_hooks
- Flutter Dva Hooks - A hook library built on top of
flutter_hooksfor Flutter Dva state management. - props
- Provides the useDvaProps hook for accessing Dva Props without subscribing to Store state changes.
- store
- Provides the useDvaStore hook for accessing the Dva Store instance without subscribing to state changes.