Flutter redux: typedef vs viewmodel property

305 Views Asked by At

I have a question about flutter_redux.

I have seen that there are two ways for passing a function to a presentational component by StoreConnector:

  • with typedef
  • as a viewmodel property

What is the difference, if any, beetween this two pieces of code?

Piece 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());
    }
    );
  }       
}

Piece 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,            
        );
      },
    );
  }
}
1

There are 1 best solutions below

0
On

Using a ViewModel allows you to pass through more than one object from your converter.