简体   繁体   English

带有 PATCH 方法的 GetX 不起作用(Flutter)

[英]GetX with PATCH method not working (Flutter)

I'm building a function to change passwords for users.我正在构建一个 function 来更改用户的密码。 The api used to change the user password is called via the PATCH method.用于修改用户密码的api是通过PATCH方法调用的。 I tested the api using Postman and it worked fine.我使用 Postman 测试了 api,它工作正常。 The problem comes when I use the GetX library and call the api through it.当我使用 GetX 库并通过它调用 api 时,问题就来了。 I checked and found that the backend is not receiving the request.我查看了一下,发现后台收不到请求。 Where does the problem happen?问题发生在哪里? Here is my code:这是我的代码:

ElevatedButton(
              onPressed: () {
                if (_key.currentState!.validate()) {
                  authController.changePassword(newPasswordEditingController.text).then((status) {
                    if (status.isSuccess) {
                      Navigator.pushNamedAndRemoveUntil(context,
                          doneResetPasswordScreenRoute, (route) => false);
                    } else {
                      // showCustomSnackBar("Error!");
                    }
                  });
                }
              },
              child: const Text("Change password"),
            ),

Method change password in AuthController:在 AuthController 中更改密码的方法:

Future<ResponseModel> changePassword(String newPassword) async {
      Response response = await authRepo.changePassword(getCurrentPassword(), newPassword);
      late ResponseModel responseModel;
      if (response.statusCode == 200) {
        authRepo.saveUserToken(response.body["token"]);
        authRepo.saveNewPassword(newPassword);
        responseModel = ResponseModel(true, response.body["token"]);
      } else {
        responseModel = ResponseModel(false, response.statusText!);
      }
      update();
      return responseModel;
    }

AuthRepo:授权库:

Future<Response> changePassword(String currentPassword, String newPassword) async {
    return await apiClient.patchData(AppConstants.CHANGE_PASSWORD_URI, {"passwordCurrent": currentPassword, "password": newPassword, "passwordConfirm": newPassword});
  }

ApiClient:客户端:

Future<Response> patchData(String uri, dynamic body) async {
    try {
      Response response = await patch(uri, body, headers: _mainHeaders);
      return response;
    } catch (e) {
      return Response(statusCode: 1, statusText: e.toString());
    }
  }

patch in ApiClient using GetX library.使用 GetX 库在 ApiClient 中打补丁。 I checked in browser and it is not working.我检查了浏览器,它不工作。

不工作

Api working with Postman Api 与 Postman 一起工作

与邮递员合作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM