【发布时间】:2021-05-05 11:21:59
【问题描述】:
对于我的场景,我使用了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;
}
}
【问题讨论】:
-
你能提供一些代码吗?很难说如何帮助你。
-
添加了示例基础 api 服务类
-
您能否再具体一点您想要实现的目标?您想在使用导航器切换屏幕后终止已建立的连接?
-
是的,如果我从主页导航而不等待http响应,仍然代码正在等待主页响应完成,然后再发送新的http响应,我认为这是因为等待功能,我需要导航时终止与 http 的连接
-
client.close() 完成了工作
标签: api flutter http dart flutter-pub