如何使用 RefreshIndicator 更新 FutureBuilder 状态?
How to use RefreshIndicator to update FutureBuilder state?
我正在使用 FutureBuilder 来显示从服务器加载的数据。我只想在应用程序启动时显示一次加载状态,这就是我从 initState 调用 API 的原因。我从服务器获取的数据可能会更改并反映 UI 中的更改,我正在使用 refreshIndicator。问题是我想不出更新状态的解决方案。
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
GlobalKey<RefreshIndicatorState>();
Future<List<Photo>> _photosServer;
@override
void initState() {
super.initState();
_photosServer = ApiRest.getPhotos();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: () {
_refreshIndicatorKey.currentState.show();
await getPhotosFromServer();
...
},
child: FutureBuilder(
future: _photosServer,
builder: (BuildContext context, snapshot) {
if (snapshot.data == null) {
return Center(
child: Text('Loading...'),
);
}
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, index) => ListTile(
title: Text(snapshot.data[index].title),
),
);
},
),
),
);
}
}
在 onRefresh 函数中,我使用以下代码在从服务器获取数据时显示 RefreshIndicator。
onRefresh: () {
_refreshIndicatorKey.currentState.show();
await getPhotosFromServer();
...
}
我还应该做些什么来处理这个问题?
为了刷新数据并显示刷新指示器,只需等待结果,然后更新未来:
onRefresh: () async {
final results = await getPhotosFromServer();
setState(() {
_photosServer = Future.value( results );
});
},
您可以有一个单独的 List<Photo>
变量,它可以由 FutureBuilder 或 RefreshIndicator 更新,并执行如下操作:
class _MyHomePageState extends State<MyHomePage> {
GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
GlobalKey<RefreshIndicatorState>();
List<Photo> _photosList;
Future<void> _initPhotosData;
@override
void initState() {
super.initState();
_initPhotosData = _initPhotos();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: FutureBuilder(
future: _initPhotosData,
builder: (BuildContext context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
case ConnectionState.active:
{
return Center(
child: Text('Loading...'),
);
}
case ConnectionState.done:
{
return RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: _refreshPhotos,
child: ListView.builder(
itemCount: _photosList.length,
itemBuilder: (BuildContext context, index) => ListTile(
title: Text(_photosList[index].title),
),
));
}
}
},
),
);
}
Future<void> _initPhotos() async {
final photos = await ApiRest.getPhotos();
_photosList = photos;
}
Future<void> _refreshPhotos() async {
final photos = await ApiRest.getPhotos();
setState(() {
_photosList = photos;
});
}
}
onRefresh: () {
return Future(() { setState(() {}); });
},
在你的 RefreshIndicator 中会刷新子 FutureBuilder
我正在使用 FutureBuilder 来显示从服务器加载的数据。我只想在应用程序启动时显示一次加载状态,这就是我从 initState 调用 API 的原因。我从服务器获取的数据可能会更改并反映 UI 中的更改,我正在使用 refreshIndicator。问题是我想不出更新状态的解决方案。
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
GlobalKey<RefreshIndicatorState>();
Future<List<Photo>> _photosServer;
@override
void initState() {
super.initState();
_photosServer = ApiRest.getPhotos();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: () {
_refreshIndicatorKey.currentState.show();
await getPhotosFromServer();
...
},
child: FutureBuilder(
future: _photosServer,
builder: (BuildContext context, snapshot) {
if (snapshot.data == null) {
return Center(
child: Text('Loading...'),
);
}
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, index) => ListTile(
title: Text(snapshot.data[index].title),
),
);
},
),
),
);
}
}
在 onRefresh 函数中,我使用以下代码在从服务器获取数据时显示 RefreshIndicator。
onRefresh: () {
_refreshIndicatorKey.currentState.show();
await getPhotosFromServer();
...
}
我还应该做些什么来处理这个问题?
为了刷新数据并显示刷新指示器,只需等待结果,然后更新未来:
onRefresh: () async {
final results = await getPhotosFromServer();
setState(() {
_photosServer = Future.value( results );
});
},
您可以有一个单独的 List<Photo>
变量,它可以由 FutureBuilder 或 RefreshIndicator 更新,并执行如下操作:
class _MyHomePageState extends State<MyHomePage> {
GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
GlobalKey<RefreshIndicatorState>();
List<Photo> _photosList;
Future<void> _initPhotosData;
@override
void initState() {
super.initState();
_initPhotosData = _initPhotos();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: FutureBuilder(
future: _initPhotosData,
builder: (BuildContext context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
case ConnectionState.active:
{
return Center(
child: Text('Loading...'),
);
}
case ConnectionState.done:
{
return RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: _refreshPhotos,
child: ListView.builder(
itemCount: _photosList.length,
itemBuilder: (BuildContext context, index) => ListTile(
title: Text(_photosList[index].title),
),
));
}
}
},
),
);
}
Future<void> _initPhotos() async {
final photos = await ApiRest.getPhotos();
_photosList = photos;
}
Future<void> _refreshPhotos() async {
final photos = await ApiRest.getPhotos();
setState(() {
_photosList = photos;
});
}
}
onRefresh: () {
return Future(() { setState(() {}); });
},
在你的 RefreshIndicator 中会刷新子 FutureBuilder