Flutter - Error parsing HTML data

1,537

You should probably remove this snippet, which relies on item.location, which you aren't populating.

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

I'd refactor the Job class to rename it Article, Headline or Story i.e. something meaningful. For example:

class Story {
  String title;
  String dateAdded;

  Story(this.title, this.dateAdded);

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

Your scraping code could then be written:

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();
Share:
1,537
Jake
Author by

Jake

Work email: jake(at)squaredsoftware.co.uk Please support me by downloading my new iOS application, travelrecce!

Updated on December 06, 2022

Comments

  • Jake
    Jake over 1 year

    I've been trying to modify the code available here so that the title and dateAdded from VMoneyNews is visible in the apps listView.

    Unfortunately, when I try to run the following edited code I get the error stated in the Image below.

    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,
    
      ));
    });
    

    method 'trim' called on null

    • Jake
      Jake over 5 years
      What do I need to do here?
    • ibhavikmakwana
      ibhavikmakwana over 5 years
      can you update the cde with the full snippet, in above one there is no trim method.
    • Jake
      Jake over 5 years
      I never added a trim method, above is the only code I changed, do you recon this is the issue?
  • Jake
    Jake over 5 years
    This is just what I’m looking for, thanks for the help Richard