1

私のシナリオでは、フラッター http パッケージを使用して http リクエストを作成しました...ホーム画面で約 3 つの http リクエストを送信する必要があります。

私は BaseAPIService クラスを使用しているので、すべての API 呼び出しはそれを通過します。

上記のリクエストが発生しているときに別の場所に移動すると、その接続を破棄する方法は?? それ以外の場合は、ナビゲート後もアプリが以前の API リクエストが完了するまで待機しています。

使用される基本 API サービス クラスのサンプル

class ApiService {
  apiGet(url, data) async {
  Get.dialog(LoadingDialog());
  var response;
  if (data == null) {
    response = await http.get(
    baseUrl + url,
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    },
  );
}
Navigator.pop(Get.overlayContext);
return response;
}

apiPost(url, data) async {
  FocusScopeNode currentFocus = FocusScope.of(Get.context);
  if (!currentFocus.hasPrimaryFocus) {
  currentFocus.unfocus();
  }
  Get.dialog(LoadingDialog());
  var response;
  if (data != null) {
   response = await http.post(baseUrl + url,
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
      body: data);
}
if (data == null) {
  response = await http.post(
    baseUrl + url,
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    },
  );
}
Navigator.pop(Get.overlayContext);
return response;
}
}
4

2 に答える 2