我最近开始使用飞镖和扑扑开发用于动物收容所的应用程序,但遇到了问题。
想法是有一个捐赠页面,用户可以在其中选择为狗买食物。打开的脚手架将显示食物的图片,并从销售所述食物的网站和当前
价格中获取一些数据。该图像作为资产存储在本地,但是我想至少从网站上获取价格,因此它始终是最新的。
我按照flutter.io上的教程进行操作,但是问题是http.get返回网站的整个代码,而不仅仅是我指定的部分(description_tab)。我尝试检查async&dart:convert文档,但到目前为止,我还没有发现任何可以使用div标签从HTML站点抓取特定内容的内容。
完整的代码可以在这里找到:https ://github.com/kergefarkas/osszefogasaszanhuzokert
这就是我能够获取网站的整个HTML代码的方式:
Future 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 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(
future: fetchPost(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return new Text('');
} else if (snapshot.hasError) {
return new Text('Error');
}
return new CircularProgressIndicator();
})),
我还认为JSON.decode不能与HTML代码一起使用,但是找不到任何可以使用的东西。尝试通过子字符串对其进行切片也不起作用。
感谢您的任何帮助!