From ee73ec188a7c3f58d36a708e1a08e38c896e7617 Mon Sep 17 00:00:00 2001 From: suconghou <1126045770@qq.com> Date: Thu, 21 Apr 2022 00:28:45 +0800 Subject: [PATCH] fix: state bug --- lib/ui/pages/CListVideos.dart | 52 ++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/lib/ui/pages/CListVideos.dart b/lib/ui/pages/CListVideos.dart index 7334272..2114c09 100644 --- a/lib/ui/pages/CListVideos.dart +++ b/lib/ui/pages/CListVideos.dart @@ -35,7 +35,7 @@ class _VideosListState extends State { List videoList = []; bool loading = false; String nextPageToken = ""; - + late Future loadData; bool nomore = false; Widget a = Container( @@ -60,7 +60,7 @@ class _VideosListState extends State { @override void initState() { super.initState(); - _pullToRefresh(); + loadData = _pullToRefresh(); _controller.addListener(_scrollListener); } @@ -81,21 +81,41 @@ class _VideosListState extends State { @override Widget build(BuildContext context) { final bottom = nomore ? a : b; - - return videoList.isEmpty - ? const Center(child: CircularProgressIndicator()) - : RefreshIndicator( - onRefresh: _pullToRefresh, - child: ListView( - controller: _controller, - children: [ - VideoGridWidget( - videoList, - controller: ScrollController(), + return FutureBuilder( + future: loadData, + builder: (context, AsyncSnapshot snapshot) { + if (snapshot.connectionState == ConnectionState.done) { + if (!snapshot.hasError) { + return RefreshIndicator( + onRefresh: _pullToRefresh, + child: ListView( + controller: _controller, + children: [ + VideoGridWidget( + videoList, + controller: ScrollController(), + ), + bottom, + ], + )); + } else { + return Center( + child: TextButton( + child: const Text("加载失败,点击重试"), + onPressed: () => { + setState(() { + loadData = _pullToRefresh(); + }) + }, ), - bottom, - ], - )); + ); + } + } else { + return const Center( + child: CircularProgressIndicator(), + ); + } + }); } Future _pullToRefresh() async {