我遇到一个恼人的事情,凡在我的列表视图中的第一项保留重新渲染,向上滚动时。即使我在上面。
只有这样,我注意到了这一点,是因为我有一个小部件,即上载,取一个网址,并获得元标题,描述和图片,并在一张精美的卡片显示它。
我的列表视图是相当简单:
ListView.builder(
physics: AlwaysScrollableScrollPhysics(),
controller: _scrollController,
itemCount: model.posts.posts.length,
itemBuilder: (context, index) {
// Items goes here
});
如何阻止发生呢?
,保持重新渲染窗口小部件,是一个无状态的小工具,导入一个ScopedModel模式,即从互联网上获取一些数据,并用于刮元数据,然后更新模型。
@override
Widget build(BuildContext context) {
UrlEmbedModel _model = new UrlEmbedModel(); // <-- the ScopedModel
_model.fetchHtml(url); // <-- the url param comes from the constuctor
// Rest of the widget
}
下面是从取净含量的代码。
void fetchHtml(url) {
http.get(url).then((response) {
if (response.statusCode == 200) {
// If server returns an OK response, parse the JSON
var document = parse(response.body);
var list = document.getElementsByTagName('meta');
for (var item in list) {
if (item.attributes['property'] == "og:title") {
_title = item.attributes['content'];
}
if (item.attributes['property'] == "og:description") {
_description = item.attributes['content'];
}
if (item.attributes['property'] == "og:image") {
_imageUrl = item.attributes['content'];
}
notifyListeners();
}
} else {
// If that response was not OK, throw an error.
throw Exception('Failed to load post');
}
});
}
你写的代码,看起来不错,但对于发出请求的功能?你能表现出来?
如果它是一个未来的功能,它只会发出请求一次,然后完成它,它不像甲流的功能,这将是永远听一个事件。
编辑
首先,如果这个功能发出请求,那么,职能的类型必须为Future
,void类型,如果不返回任何东西,在那之后,添加async
电话。你可以在。然后方法更改为await方法,它会更适合你。
Future<void> fetchHtml(url) async {
final Response response = await get(url);
final dynamic documents = json.decode(response.body); //import 'dart:convert';
print(documents); // print to see what you get than, pass it to the variables you want the data
if (response.statusCode == 200) {
//in here
}
}
我可以看到在读取请求的感觉的东西,如果你回答它,我会很高兴:
var documents = json.decode(response.body)
你这样做的方式,它是没有错的,但可以改善它。
找到了罪魁祸首。
这个问题不是列表视图,这是我使用的RefreshIndicator。
一旦我删除它,这个问题就走了。
这似乎是一个小部件的错误。