1

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

nvoigt
  • 75,013
  • 26
  • 93
  • 142
user3808307
  • 2,270
  • 9
  • 45
  • 99

1 Answers1

1

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!"));
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • This says Error: Expected ')' before this. Redux.store.dispatch(x => signUpAction(x, "[email protected]", "somEPa55word!")); – user3808307 Oct 26 '20 at 21:19
  • 1
    @user3808307 Sorry, forgot the brackets. – nvoigt Oct 26 '20 at 21:33
  • It seems to be working but I am having a problem with my action so I can't properly test it, I'll let you know as soon as I fix it, thank you – user3808307 Oct 26 '20 at 22:46
  • Works, I am so sorry I started with Flutter, everything is extremely complicated – user3808307 Oct 27 '20 at 02:12
  • I don't understand what it means that dispatch takes a signature and that that anonymous method matches the signature. – user3808307 Oct 27 '20 at 02:26
  • 1
    When you look at the function definition of `dispatch` you will see that it takes one parameter. That parameter must be a method that takes a store as parameter and returns a Future. Your method looked like that, so you could pass it directly as a parameter. Your new method did not look like that. So you had to first build a method to look like the old one, as far as parameter types and return type goes. That is what `() => ` or `() {}` does, it builds a method on the fly. So `(x) => code;` builds a method that gets an `x` and returns whatever `code` returns. – nvoigt Oct 27 '20 at 06:50
  • 1
    I added a documentation link so you can get a better explanation than I can put in a comment here. – nvoigt Oct 27 '20 at 06:51