繁体   English   中英

Flutter-解析HTML数据时出错

[英]Flutter - Error parsing HTML data

我一直在试图修改代码, 可在这里 ,这样的titledateAddedVMoneyNews是在应用程序可见listView

不幸的是,当我尝试运行以下已编辑的代码时,出现以下图像中指出的错误。

http.Response response = await http.get('https://www.virtualmoneysnews.com/category/bitcoin-news/');

//parse and extract the data from the web site
dom.Document document = parser.parse(response.body);
document.getElementsByTagName('article').forEach((child) {
  jobsList.add(Job(
    title: child.getElementsByClassName('title').first.text,
    dateAdded: child.getElementsByClassName('thetime date updated').last.text,

  ));
});

方法'trim'在null上调用

您可能应该删除此片段,该片段依赖于您没有填充的item.location

            Text(
              item.location.trim(),
              style: TextStyle(
                  color: Colors.white, fontWeight: FontWeight.bold),
            ),

我将重构Job类以将其重命名为ArticleHeadlineStory即有意义的东西。 例如:

class Story {
  String title;
  String dateAdded;

  Story(this.title, this.dateAdded);

  @override
  String toString() => '$title $dateAdded';
}

然后可以编写您的抓取代码:

http.Response response = await http
    .get('https://www.virtualmoneysnews.com/category/bitcoin-news/');
dom.Document document = parser.parse(response.body);

List<Story> stories = document
    .getElementsByTagName('article')
    .map((e) => Story(
          e.getElementsByClassName('title').first.text,
          e.getElementsByClassName('thetime date updated').last.text,
        ))
    .toList();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM