我对 Flutter 没有那么多经验。
我正在创建一个帐户信息页面。在该页面上,用户可以更新他们的详细信息。我已经创建了所需的 API 服务和控制器。
当我运行代码时,出现以下错误。
Too many positional arguments: 0 allowed, but 6 found.
Try removing the extra positional arguments.
productController.customerUpdate(
^
如何解决这个问题?
API服务:
Future<Response?> customerUpdate(String? email, String? firstName,
String? lastName, String? phone, String? customerAccessToken) async {
var uri = '$_url/update-customer';
print(uri);
// String token = box.read('token');
// dio.options.headers["Authorization"] = "Bearer $token";
try {
final response = await dio.patch(uri, data: {
'email': email,
'firstName': firstName,
'lastName': lastName,
'phone': phone,
'customerAccessToken': box.read('token')
});
if (response.statusCode == 200) {
box.write('token', response.data['token']);
box.write('information_updated', true);
}
return response;
} on DioError catch (e) {
debugPrint(e.response.toString());
return e.response;
}
}
产品负责人:
customerUpdate({
@required String? email,
String? firstName,
String? lastName,
String? phone,
String? customerAccessToken,
context}) async {
loader('Saving...');
dio.Response? response = await remoteServices.customerUpdate(
email, firstName, lastName, phone, customerAccessToken);
if (response!.statusCode == 200) {
Get.back();
Get.back();
myDialog.showFloatingFlushbar(
context,
'Information Updated', Color(0xff734FE0), Color(0xff734FE0));
} else {
myDialog.showFloatingFlushbar(context, 'Please check the errors above',
Colors.red.shade800, Colors.red.shade700);
}
}
代码:
final box = GetStorage();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _firstNameController = TextEditingController();
final TextEditingController _lastNameController = TextEditingController();
final TextEditingController _phoneController = TextEditingController();
customerUpdate() {
productController.customerUpdate(
email: _emailController.text,
firstName: _firstNameController.text,
lastName: _lastNameController.text,
phone: _phoneController.text,
customerAccessToken: box.read('token'),
context: context);
}
错误消息“太多位置参数”意味着方法或函数需要特定数量的特定顺序的参数,但您传递的参数超出预期。
在您的情况下,发生错误是因为您将六个参数传递给
customerUpdate
类的productController
方法,该方法被定义为仅接收五个参数。 customerUpdate
的 productController
方法应更新为根据需要包含 context
参数:
customerUpdate({
@required String? email,
String? firstName,
String? lastName,
String? phone,
String? customerAccessToken,
@required BuildContext context}) async {
// Your implementation here
}
然后,在您的
customerUpdate
函数中,更新调用以传递 context
参数:
customerUpdate() {
productController.customerUpdate(
email: _emailController.text,
firstName: _firstNameController.text,
lastName: _lastNameController.text,
phone: _phoneController.text,
customerAccessToken: box.read('token'),
context: context); // Pass the context argument here
}