Search code examples
flutteractionflutter-redux

How to pass parameters to action in Flutter


I have this action

Future<void> signUpAction(Store<AppState> store) async {
  try {
    // ...
  } catch (e) {
    // ..
  }
}

And I dispatch it like this

store.dispatch(signUpAction);

Now, if I want to pass two paramters, how would I do that? Since there is already one parameter there.

I tried this

Future<void> signUpAction(Store<AppState> store, email, password) async {
  try {
    // ...
  } catch (e) {
    // ..
  }
}

but then on dispatching, if I do

store.dispatch(signUpAction("[email protected]", "somEPa55word!"));

it says the singUpAction expects 3 parameters, so I don't know very well how to pass only these two

Thank you


Solution

  • The dispatch method expects a specific signature. If your method does not have exactly that signature, you can make an anonymous function on the fly that matches the signature.

    In this case, since your method takes not only the store, but also the email and password:

    store.dispatch((x) => signUpAction(x, "[email protected]", "somEPa55word!"));