如何在 FutureBuilder() 中正确 "refresh" Widgets

How to properly "refresh" Widgets inside FutureBuilder()

我在 FutureBuilder() 中有一个 Listview.builder(),它显示从 API 获取的数据。我可以成功检索数据。但是当我调用 refreshData() 函数时,以前的数据会附加到列表中。我如何正确 'refresh' FutureBuilder() 中的小部件?

注意:我这里只使用了get请求,所以数据不可能在后端被复制。所以问题实际上出在显示数据上。

这是我的代码:

class _MyHomePageState extends State<MyHomePage> {
  List<Giver> _givers = [];
  Future giversList;

  getData() async {
    _givers.addAll(await NetworkHelper().fetchGivers());
    return _givers;
  }

  refreshData() {
    giversList = getData();
  }

  @override
  void initState() {
    super.initState();
    giversList = getData();
  }

  @override
   Widget build(BuildContext context) {
     return Scaffold(
       body: SafeArea(
       child: Column(
         children: <Widget>[
            RaisedButton(
              onPressed: (){
                setState(() {
                refreshData();
              });
            },
          child: Text('Refresh'),
        ),
        FutureBuilder(
          future: giversList,
          builder: (context, snapShot){
            switch(snapShot.connectionState) {
              case ConnectionState.none:
                return Center(child: Text('none'));

              case ConnectionState.active:

              case ConnectionState.waiting:
                return Center(child: CircularProgressIndicator());

              //this is where the listview is created
              case ConnectionState.done:
                return ListView.builder(
                  shrinkWrap: true,
                  itemCount: _givers.length,
                  itemBuilder: (context, index) {
                    return ListTile(
                      title: Text(snapShot.data[index].name),
                      subtitle: Text(snapShot.data[index].address),
                    );
                  });

                  default:
                   return Center(child: Text('Default!'));
                 }
               },
             )
           ],
         ),
       ),
     );
   }
}

正如@pskink 在上面的评论中提到的,我只是将 _givers.addAll(await NetworkHelper().fetchGivers()); 替换为 _givers = await NetworkHelper().fetchGivers();

感谢您的帮助!