Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 作用域模型-接收器:闭包:({dynamic formData})=>;功能无效';登录';_Dart_Flutter - Fatal编程技术网

Dart 作用域模型-接收器:闭包:({dynamic formData})=>;功能无效';登录';

Dart 作用域模型-接收器:闭包:({dynamic formData})=>;功能无效';登录';,dart,flutter,Dart,Flutter,我试图实现ScopedModel,我有这样的代码示例,没有任何问题,但是当我尝试实现相同的算法时,我得到了错误。以下是您需要的东西: 登录按钮代码块: void _submitForm(Function authenticate) async { _formKey.currentState.save(); print(_formData); http.Response response = await authenticate(_formData); } void

我试图实现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);
  }
作用域模型登录代码块:

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);
  }
错误:

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\u submitForm(函数(映射)身份验证)异步{
_formKey.currentState.save();
打印(_formData);
http.Response-Response=wait-authenticate(_-formData);
}
我可能错了,但我认为您无法传递包含命名参数的函数,因此您的作用域模型代码需要:

void login(Map<String, dynamic> formData) async {
  http.Response response = await http.post(
    url,
    body: formData,
  );
  print(response.body);
}
void登录(映射formData)异步{
http.Response-Response=等待http.post(
网址,
正文:formData,
);
打印(响应.正文);
}

错误是因为您将一个带参数的函数传递给一个方法,该方法需要一个不带参数的函数

试试这个:

void _submitForm(Function(Map<String, dynamic>) authenticate) async {
  _formKey.currentState.save();
  print(_formData);
  http.Response response = await authenticate(_formData);
}
void\u submitForm(函数(映射)身份验证)异步{
_formKey.currentState.save();
打印(_formData);
http.Response-Response=wait-authenticate(_-formData);
}
我可能错了,但我认为您无法传递包含命名参数的函数,因此您的作用域模型代码需要:

void login(Map<String, dynamic> formData) async {
  http.Response response = await http.post(
    url,
    body: formData,
  );
  print(response.body);
}
void登录(映射formData)异步{
http.Response-Response=等待http.post(
网址,
正文:formData,
);
打印(响应.正文);
}

命名参数是问题所在。非常感谢。您不需要指定函数(数据类型),只需函数正在工作。命名参数是问题所在。非常感谢。您不需要指定函数(数据类型),只需函数工作即可。