Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to call dispatch action from a void method in flutter redux #122

Closed
hkmsadek opened this issue Apr 2, 2019 · 1 comment
Closed

how to call dispatch action from a void method in flutter redux #122

hkmsadek opened this issue Apr 2, 2019 · 1 comment

Comments

@hkmsadek
Copy link

hkmsadek commented Apr 2, 2019

I am trying to perform a dispatch action from a method. Here is how I am trying

StoreConnector<AppState, AppState>(
                   converter: (store) => store.state,
                   builder: (context, items) => Column(
                      children: <Widget>[
                        Text(items.rahi),
                        Text(items.mySiteUrl),
                        RaisedButton(
                          child: Text('update rahi'),
                         onPressed: (){_updateRahi(store);},
                        ),
                      ],
                   )

                 ),

You can see I have cakked updateRahi method and inside this method

void _updateRahi(store){
  var text = status.text; 
  // want to call dispatch action from here
  store.dispatch('this is some texts');

}
@brianegan
Copy link
Owner

Hey there -- please see the following discussion: #108

Essentially, you need to convert the Store into a ViewModel like so:

/// Just returns a function that takes in an integer and dispatches an `ItemSelectedAction`
StoreConnector<AppState, void Function(int selectedItemId)>(
  converter: (store) => (int selectedItemId) => store.dispatch(ItemSelectedAction(selectedItemId)),
  builder: (context, onItemSelected) => RaisedButton(
    child: Text("Press me!"),
    onPressed: () => onItemSelected(1),
  ),
);


/// More complex example with a "ViewModel"
class _ViewModel {
  final int itemId;
  final void Function() onItemSelected;

  _ViewModel(this.itemId, this.onItemSelected);
}

StoreConnector<AppState, _ViewModel>(
  converter: (store) {
    final itemId = store.state.getMeAnItemId;
    
    return _ViewModel(
      itemId,
      () => store.dispatch(ItemSelectedAction(itemId)),
    );
  },
  builder: (context, viewModel) => RaisedButton(
    child: Text("Displaying ${viewModel.itemId}"),
    onPressed: viewModel.onItemSelected,
  ),
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants