I'm trying to implement ScopedModel, I have code example works like this without any problem but when I try to implement the same algorithm I'm getting the error. Here the things you need:
Login button code block:
void _submitForm(Function authenticate) async {
_formKey.currentState.save();
print(_formData);
http.Response response = await authenticate(_formData);
}
scoped model login code block:
void login({Map<String, dynamic> formData}) async {
http.Response response = await http.post(
url,
body: formData,
);
print(response.body);
}
onPressed code:
onPressed: () => _submitForm(model.login),
Error:
NoSuchMethodError: Closure call with mismatched arguments: function '_MainModel&Model&ConnectedModel&AuthModel.login'
E/flutter (13496): Receiver: Closure: ({dynamic formData}) => void from Function 'login':.
E/flutter (13496): Tried calling: _MainModel&Model&ConnectedModel&AuthModel.login(_LinkedHashMap len:3)
E/flutter (13496): Found: _MainModel&Model&ConnectedModel&AuthModel.login({dynamic formData}) => void
I tried to change type of methods etc. Nothing worked properly.
I'm open to different style of implementations via Scoped Model.
Solution 1: Jordan Davies
The error is because you're passing in a function that takes a parameter to a method that expects a function that takes no parameters.
Try this:
void _submitForm(Function(Map<String, dynamic>) authenticate) async {
_formKey.currentState.save();
print(_formData);
http.Response response = await authenticate(_formData);
}
I may be wrong but I don't think you can pass around functions that contain named parameters, so your scoped model code would need to be:
void login(Map<String, dynamic> formData) async {
http.Response response = await http.post(
url,
body: formData,
);
print(response.body);
}