对于我的场景,我使用了flutter http包来发出http请求......在主屏幕上我必须发送大约3个http请求,因为我不得不使用await请求一个一个发送。
我使用了 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;
}
}