1

我有一个关于flutter_redux.

我已经看到有两种方法可以通过以下方式将函数传递给演示组件StoreConnector

  • typedef
  • 作为视图模型属性

这两段代码之间有什么区别(如果有的话)?

第 1 部分:

class ExampleContainer extends StatelessWidget {

  ExampleContainer({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, _ViewModel>(    
      converter: _ViewModel.fromStore,
      builder: (context, vm) {
        return ExampleScreen(
          exampleAction: vm.exampleAction,            
        );
      },
    );
  }
}

class _ViewModel {
  final Function() exampleAction;

  _ViewModel({this.exampleAction});

  static _ViewModel fromStore(Store<AppState> store) {
    return _ViewModel(exampleAction: () {
      store.dispatch(ExampleAction());
    }
    );
  }       
}

第 2 部分:

typedef ExampleCallback = Function();

class ExampleContainer extends StatelessWidget {

  ExampleContainer({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, ExampleCallback>(    
      converter: (Store<AppState> store) {
        return () {
          store.dispatch(ExampleAction());
        };
      },
      builder: (BuildContext context, ExampleCallback exampleCallback) {
        return ExampleScreen(
          exampleAction: exampleCallback,            
        );
      },
    );
  }
}
4

1 回答 1

0

使用 ViewModel 允许您从converter.

于 2020-10-28T11:21:43.703 回答