2

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

3
  • What do I need to do here? Commented Aug 17, 2018 at 16:35
  • can you update the cde with the full snippet, in above one there is no trim method. Commented Aug 17, 2018 at 16:35
  • I never added a trim method, above is the only code I changed, do you recon this is the issue? Commented Aug 17, 2018 at 16:36

1 Answer 1

3

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();
Sign up to request clarification or add additional context in comments.

1 Comment

This is just what I’m looking for, thanks for the help Richard

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.