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

flutter_dva hooks

flutter_dva_hooks #

pub package GitHub license

Flutter Dva Hooks — 基于 flutter_hooks 封装的 Flutter Dva 状态管理 Hook 库,让你在 Flutter 中以声明式、函数式的方式使用 Dva 状态管理。

English | 中文


📦 Installation #

Add flutter_dva_hooks to your pubspec.yaml:

dependencies:
  flutter_dva_hooks: ^1.0.1

Or install via command:

flutter pub add flutter_dva_hooks

✨ Features #

Hook Description
useDvaConnect 监听 Store 状态变化并自动更新页面,同时获取 Props 和 Store
useDvaProps 仅获取 Props,不监听 Store 变化
useDvaStore 仅获取 Store 实例,不监听 Store 变化

1. useDvaConnect — Full connection with auto-refresh #

Automatically subscribe to Store state changes and trigger 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 #

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 #

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, and store
  • Props: Contains dispatch, history, and namespace-related properties
  • Store: The Store instance from flutter_dva

🔗 Dependencies #


📄 License #

This project is licensed under the MIT License.