作用域模型 - 接收机:封闭:({动态FORMDATA})=>从功能 '登录' 空隙

问题描述 投票:0回答:1

我试图实现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

我试图改变的方法等没有适当的工作类型。

我打开不同的风格通过作用域模型实现的。

dart flutter
1个回答
1
投票

这个错误是因为你正在传递,需要一个参数给需要的函数不带参数的方法的功能。

尝试这个:

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.