我希望您能检查URL是否存在。
功能:
Future _checkUrl(String url) async {
http.Response _urlResponse = await http.get(Uri.parse(url));
if (_urlResponse.statusCode == 200) {
return true;
}
else {
return false;
}
}
调用:
_checkUrl("https://stackoverf").then((value) => {
print(value)
});
当我输入 https://fonts.google.com/?category=Sans+Serif
时它有效(返回 true),或者输入 https://stackoverflow.com/qu
时也有效(返回 false)。
但是,当我尝试使用无效的网址 https://stackoverf
时,会出现错误信息:[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: SocketException: Failed host lookup: 'stackoverf' (OS Error: No address associated with hostname, errno = 7)
。
如何使_checkUrl
能够在这种情况下返回false呢?