gpt4 book ai didi

dart - 有没有一种方法可以使用我的刷新指示器在错误快照之后刷新 future builder

转载 作者:行者123 更新时间:2023-12-03 04:02:25 26 4
gpt4 key购买 nike

我正在从Newsapi.org提取数据,我需要能够在出现快照错误后重新加载futurebuilder(),所以我非常陌生,所以这听起来很奇怪。
我已经能够获取我的数据,并且还尝试将 call 重新放入if(snapshot.hasError) {}但是我无法使它工作

Widget build(BuildContext context) {
var refreshIndicator = RefreshIndicator(
key: refreshKey,
child: FutureBuilder<List<Source>>(
future: list_sources,
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text(
'An error occured, check your internet connection and try again');
}
if (snapshot.hasData) {
if (snapshot.data != null) {
List<Source> sources = snapshot.data;
return new ListView(
children: sources
.map((source) => GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
ArticleScreen(source: source)));
},
child: Card(
elevation: 1.0,
color: Colors.white,
margin: const EdgeInsets.symmetric(
vertical: 8.0, horizontal: 14.0),
child: Row(
crossAxisAlignment:
CrossAxisAlignment.start,
children: <Widget>[
Container(
margin: const EdgeInsets.symmetric(
vertical: 20.0, horizontal: 4.0),
width: 100.0,
height: 140.0,
child: Image.asset(
"lib/images/newspaper 2.png"),
),
Expanded(
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: Container(
margin:
const EdgeInsets.only(
top: 20.0,
bottom: 10.0),
child: Text(
'${source.name}',
style: TextStyle(
fontSize: 18.0,
fontWeight:
FontWeight
.bold),
),
),
),
],
),
Container(
child: Text(
'${source.description}',
style: TextStyle(
fontSize: 12.0,
fontWeight:
FontWeight.bold,
color: Colors.grey),
),
),
Container(
child: Text(
'${source.category}',
style: TextStyle(
fontSize: 14.0,
fontWeight:
FontWeight.bold,
color: Colors.black),
),
),
],
),
),
],
),
),
))
.toList());
}
} else {
return CircularProgressIndicator();
}
},
),
onRefresh: refreshListSource,
);
我希望从屏幕顶部开始滑动并出现错误,以尝试重新加载数据

最佳答案

@Sebastian将refreshKey置于build方法之外,重建时屏幕不会回到顶部。

  var refreshKey = GlobalKey<RefreshIndicatorState>();

@override
Widget build(BuildContext context) {
...
}

关于dart - 有没有一种方法可以使用我的刷新指示器在错误快照之后刷新 future builder ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54459621/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com