http get on dart需要很长时间才能返回响应状态
我正在使用此方法检查应用程序是否可以访问目标urlhttp get on dart需要很长时间才能返回响应状态,http,flutter,dart,Http,Flutter,Dart,我正在使用此方法检查应用程序是否可以访问目标url Future<bool> ip() async { var url = 'http://192.168.2.176/applications.html';try { http.Response response = await http.get((url));return true;} catch (_) {print('WTF'); return false;}} Future ip()异步{ var url='1〕http:/
Future<bool> ip() async {
var url = 'http://192.168.2.176/applications.html';try {
http.Response response = await http.get((url));return true;} catch (_) {print('WTF');
return false;}}
Future ip()异步{
var url='1〕http://192.168.2.176/applications.html”“试试看{
Response Response=wait http.get((url));返回true;}catch({print('WTF');
返回false;}}
我的问题是,如果响应失败,函数何时返回false
然后需要大约30秒才能将布尔结果作为假值提供给我。
有没有办法解决这个问题,,
如果url不可访问,我需要函数在5秒内返回false。请检查此代码,它可能会帮助您:
try {
final request = await client.get(...);
final response = await request.close()
.timeout(const Duration(seconds: 2));
// rest of the code
...
} on TimeoutException catch (_) {
// A timeout occurred.
} on SocketException catch (_) {
// Other exception
}
相应地修改您的代码,让我知道它是否工作。使用较低级别的http客户端并设置其连接超时。非常感谢你的帮助。我现在就用,非常感谢