Flutter 发生异常。SocketException(SocketException:主机查找失败:';字符串';(操作系统错误:没有与主机名关联的地址,errno=7))
我正在用flatter编写一个模块,它返回指定URL的预览。但是它返回以下错误?如何解决这个问题 我得到的错误是: 发生异常。 SocketException(SocketException:主机查找失败:“字符串”(操作系统错误:没有与主机名关联的地址,Errno=7)) 我尝试了以下方法 代码:Flutter 发生异常。SocketException(SocketException:主机查找失败:';字符串';(操作系统错误:没有与主机名关联的地址,errno=7)),flutter,Flutter,我正在用flatter编写一个模块,它返回指定URL的预览。但是它返回以下错误?如何解决这个问题 我得到的错误是: 发生异常。 SocketException(SocketException:主机查找失败:“字符串”(操作系统错误:没有与主机名关联的地址,Errno=7)) 我尝试了以下方法 代码: import 'package:html/parser.dart'; import 'package:http/http.dart'; class FetchPreview { Future
import 'package:html/parser.dart';
import 'package:http/http.dart';
class FetchPreview {
Future fetch(url) async {
final client = Client();
final response = await client.get(_validateUrl(url));
final document = parse(response.body);
String description, title, image, appleIcon, favIcon;
var elements = document.getElementsByTagName('meta');
final linkElements = document.getElementsByTagName('link');
elements.forEach((tmp) {
if (tmp.attributes['property'] == 'og:title') {
//fetch seo title
title = tmp.attributes['content'];
}
//if seo title is empty then fetch normal title
if (title == null || title.isEmpty) {
title = document.getElementsByTagName('title')[0].text;
}
//fetch seo description
if (tmp.attributes['property'] == 'og:description') {
description = tmp.attributes['content'];
}
//if seo description is empty then fetch normal description.
if (description == null || description.isEmpty) {
//fetch base title
if (tmp.attributes['name'] == 'description') {
description = tmp.attributes['content'];
}
}
//fetch image
if (tmp.attributes['property'] == 'og:image') {
image = tmp.attributes['content'];
}
});
linkElements.forEach((tmp) {
if (tmp.attributes['rel'] == 'apple-touch-icon') {
appleIcon = tmp.attributes['href'];
}
if (tmp.attributes['rel']?.contains('icon') == true) {
favIcon = tmp.attributes['href'];
}
});
return {
'title': title ?? '',
'description': description ?? '',
'image': image ?? '',
'appleIcon': appleIcon ?? '',
'favIcon': favIcon ?? ''
};
}
_validateUrl(String url) {
if (url?.startsWith('http://') == true ||
url?.startsWith('https://') == true) {
return url;
} else {
return 'http://$url';
}
}
}