我试图实现ScopedModel,我的代码示例是这样的,没有任何问题,但是当我尝试实现相同的算法,我发现了错误。在这里,你需要的东西:
登录按钮的代码块:
void _submitForm(Function authenticate) async {
_formKey.currentState.save();
print(_formData);
http.Response response = await authenticate(_formData);
}
作用域模型登录代码块:
void login({Map<String, dynamic> formData}) async {
http.Response response = await http.post(
url,
body: formData,
);
print(response.body);
}
onPressed代码:
onPressed: () => _submitForm(model.login),
错误:
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
我试图改变的方法等没有适当的工作类型。
我打开不同的风格通过作用域模型实现的。
这个错误是因为你正在传递,需要一个参数给需要的函数不带参数的方法的功能。
尝试这个:
void _submitForm(Function(Map<String, dynamic>) authenticate) async {
_formKey.currentState.save();
print(_formData);
http.Response response = await authenticate(_formData);
}
我可能是错的,但我不认为你可以绕过包含命名参数的功能,让你的作用域模型代码将需要:
void login(Map<String, dynamic> formData) async {
http.Response response = await http.post(
url,
body: formData,
);
print(response.body);
}