如何使用 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();

           ...

        }

我还应该做些什么来处理这个问题?

解决方法

您可以有一个单独的 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: () async {
  final results = await getPhotosFromServer();
  setState(() {
    _photosServer = Future.value( results );
  });
},