如何使用 RefreshIndicator 更新 FutureBuilder 状态?

时间:2021-01-12 10:50:30

标签: flutter state pull-to-refresh flutter-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();

           ...

        }

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

2 个答案:

答案 0 :(得分:1)

您可以有一个单独的 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;
    });
  }
}

答案 1 :(得分:-1)

为了刷新数据并显示刷新指示器,只需等待结果然后更新未来:

onRefresh: () async {
  final results = await getPhotosFromServer();
  setState(() {
    _photosServer = Future.value( results );
  });
},