I recently started developing an app for an animal shelter using dart & flutter, and ran into problem.
The idea is that there is a donation page, where the user can choose to buy food for the dogs. The opening scaffold would show the picture of the food, and fetch some data from a website that sells said food, and the current price. The image is stored locally as an asset, but I'd like to fetch at least the price from the website, so it is always up to date.
I followed the tutorial on flutter.io, but the problem is that the http.get returns the whole code of the site, not just the portion I specified (description_tab). I tried checking the async & dart:convert documentation, but so far I haven't found anything that could help in scraping specific content from a HTML site, using the div tag.
The full code can be found here: https://github.com/kergefarkas/osszefogasaszanhuzokert
This is how I was able to fetch the whole HTML code of the site:
Future<JoseraAdultActive> fetchPost() async {
final response = await http.get(
'https://www.petissimo.hu/kutyaknak/szarazeledelek/josera/josera-active.html');
final json = JSON.decode(response.body);
return new JoseraAdultActive.fromJson(json);
}
class JoseraAdultActive {
final String description;
final String price;
JoseraAdultActive({this.description, this.price});
factory JoseraAdultActive.fromJson(Map<String, dynamic> json) {
return new JoseraAdultActive(
description: json['description_tab'],
price: json['product_price_from']);
}
}
And this is where I would display the extracted information:
new Container(
padding: const EdgeInsets.only(top: 10.0, right: 5.0),
child: new FutureBuilder<JoseraAdultActive>(
future: fetchPost(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return new Text('');
} else if (snapshot.hasError) {
return new Text('Error');
}
return new CircularProgressIndicator();
})),
I also think that the JSON.decode won't work with a HTML code, but couldn't find anything to use instead. Trying to slice it via substrings didn't work either.
Thanks for any help in advance!