Getting 'Future<String?>' is not a subtype of type 'String' 错误为什么要在 flutter 应用程序中从网站抓取数据?
Getting 'Future<String?>' is not a subtype of type 'String' error why scraping data from a website in flutter app?
[此图片包含我从网站获取数据的部分代码 https://arprogramming.blogspot.com/ and storing the data in 3 separate lists. The link list is used to store the link of the blog so that I can use it as a link afterwards to redirect to the site from the app]2
These are all my imports
Thia is my pubspec.yaml file
This is the part of code where I am using the scraped data
THIS IS MY ERROR
下面是我的主要代码
Future<void> _getDataFromWeb() async{
var uri =Uri.parse('https://arprogramming.blogspot.com/');
final response = await http.get(uri);
dom.Document document = parser.parse(response.body);
final elements = document.getElementsByClassName('entry-title');
final content = document.getElementsByClassName('entry-content');
final link1 = document.getElementsByClassName('entry-header blog-entry-header');
setState(() {
title = elements.map((elements)=> elements.getElementsByTagName("a")[0].innerHtml.toString()).toList();
post = content.map((content)=> content.getElementsByTagName("p")[0].innerHtml.toString()).toList();
link = link1.map((link1) async => link1.getElementsByTagName("a")[0].attributes['href']).cast<String>().toList();
});
}
您不能将 Future 用作字符串。因为这些在你想用的时候不一定可用。使用“then”,它让我们知道异步函数何时结束,我们有变量可以使用。
注意:请更加小心进一步的存储库。社区应该清楚地上传他们的代码作为代码片段和错误消息。欢迎
http.get(uri).then((String response){
dom.Document document = parser.parse(response.body);
final elements = document.getElementsByClassName('entry-title');
final content = document.getElementsByClassName('entry-content');
final link1 = document.getElementsByClassName('entry-header blog-entry-header');
setState(() {
title = elements.map((elements)=> elements.getElementsByTagName("a")[0].innerHtml.toString()).toList();
post = content.map((content)=> content.getElementsByTagName("p")[0].innerHtml.toString()).toList();
link = link1.map((link1) async => link1.getElementsByTagName("a")[0].attributes['href']).cast<String>().toList();
});
});
[此图片包含我从网站获取数据的部分代码 https://arprogramming.blogspot.com/ and storing the data in 3 separate lists. The link list is used to store the link of the blog so that I can use it as a link afterwards to redirect to the site from the app]2
These are all my imports
Thia is my pubspec.yaml file
This is the part of code where I am using the scraped data
THIS IS MY ERROR 下面是我的主要代码
Future<void> _getDataFromWeb() async{
var uri =Uri.parse('https://arprogramming.blogspot.com/');
final response = await http.get(uri);
dom.Document document = parser.parse(response.body);
final elements = document.getElementsByClassName('entry-title');
final content = document.getElementsByClassName('entry-content');
final link1 = document.getElementsByClassName('entry-header blog-entry-header');
setState(() {
title = elements.map((elements)=> elements.getElementsByTagName("a")[0].innerHtml.toString()).toList();
post = content.map((content)=> content.getElementsByTagName("p")[0].innerHtml.toString()).toList();
link = link1.map((link1) async => link1.getElementsByTagName("a")[0].attributes['href']).cast<String>().toList();
});
}
您不能将 Future 用作字符串。因为这些在你想用的时候不一定可用。使用“then”,它让我们知道异步函数何时结束,我们有变量可以使用。
注意:请更加小心进一步的存储库。社区应该清楚地上传他们的代码作为代码片段和错误消息。欢迎
http.get(uri).then((String response){
dom.Document document = parser.parse(response.body);
final elements = document.getElementsByClassName('entry-title');
final content = document.getElementsByClassName('entry-content');
final link1 = document.getElementsByClassName('entry-header blog-entry-header');
setState(() {
title = elements.map((elements)=> elements.getElementsByTagName("a")[0].innerHtml.toString()).toList();
post = content.map((content)=> content.getElementsByTagName("p")[0].innerHtml.toString()).toList();
link = link1.map((link1) async => link1.getElementsByTagName("a")[0].attributes['href']).cast<String>().toList();
});
});