使用dio调用api会产生404抖动

时间:2020-03-30 22:29:56

标签: http flutter flutter-dependencies dio

我正在从post类型的flutter调用api,但它给我错误404,可以找出问题所在

class OtpVerifyApi {
  Future otpVerifyAPI(OtpVerifyRequest otpVerifyRequest, String token) async {
    return NetworkCommon.getClient(Constants.apiEndPoint)
        .post("otp",
            data: otpVerifyRequest.toMap(),
            options: new Options(headers: {"Authorization": token}))
        .then((response) {
      return response.data;
    }).catchError((onError) {
      print("onError " + onError.toString());
    });
  }
}

class OtpVerifyRequest {
  String otp;

  toMap() {
    return {"otp": this.otp};
  }
}



verfiyUser(OtpVerifyRequest otpVerifyRequest, String token) async {
    _memoizer.runOnce(() =>
        OtpVerifyApi().otpVerifyAPI(otpVerifyRequest, token).then((reponse) {
          OtpVerifyResponse otpVerifyResponse =
              OtpVerifyResponse.fromJson(reponse);
          print("otpVerifyResponse $otpVerifyResponse");

        }).catchError((error) {
          hideLoader();
          print("otp error: $error");
        }));
  }

这些是我的三个源文件,第一个是我的客户端,第二个是我的请求正文,第三个是我得到响应的位置。 帮我解决这个问题。 预先感谢。

Future<OtpVerifyResponse> verifyUser(
      OtpVerifyRequest otpVerifyRequest, String token) async {
    Map<String, String>  requestHeaders = {
        "Accept": "application/json",
        "Authorization": "Bearer $token",
      };
    return http
        .post(Constants.apiEndPoint + "registration/_verify",
            body: otpVerifyRequest.toMap(), headers: requestHeaders)
        .then((http.Response response) {
      hideLoader();
      final int statusCode = response.statusCode;
      print("statusCode" + statusCode.toString());
      print("response ${json.decode(response.body)}");
      if (statusCode < 200 || statusCode > 400 || json == null) {
        hideLoader();
        throw new Exception("Error while fetching data");
      }
      return OtpVerifyResponse.fromJson(json.decode(response.body));
    });
  }

我也尝试了这种http方法,但也不能确定我在做什么错

1 个答案:

答案 0 :(得分:0)

class OtpVerifyApi {
  Future otpVerifyAPI(OtpVerifyRequest otpVerifyRequest, String token) async {
    return NetworkCommon.getClient(Constants.apiEndPoint)
        .post("otp",
            data: otpVerifyRequest.toMap(),
            options: new Options(headers: {"Authorization": "Bearer $token"}))
        .then((response) {
      return response.data;
    }).catchError((onError) {
      print("onError " + onError.toString());
    });
  }
}

我以错误的方式传递了授权令牌。.花了一段时间才弄清楚:)