我不能在FutureBuilder中使用RefreshIndicator吗?

时间:2020-08-18 19:42:31

标签: flutter dart

我正在尝试将 RefreshIndicator FutureBuilder 结合使用,但是当我取消刷新操作后,从数据库中删除一行后数据未更改,这似乎不起作用

RefreshIndicator(
          onRefresh: () async {
            return await carProvider.getCar();
          },
          child: FutureBuilder<List<Car>>(
            future: carProvider.getCar(),
            // initialData: carProvider.carList,
            builder:
                (BuildContext context, AsyncSnapshot<List<Car>> snapshot) {
              print(snapshot.connectionState);
              if (snapshot.connectionState == ConnectionState.waiting)
                return CircularProgressIndicator();
              else if (snapshot.connectionState == ConnectionState.done &&
                  snapshot.data.length > 0)
                return ListView.builder(
                  itemCount: snapshot.data.length,
                  shrinkWrap: true,
                  itemBuilder: (context, index) => Row(
                    children: [
                      Text(snapshot.data[index].name),
                      RaisedButton(
                        child: Text('Remove'),
                        onPressed: () {
                          carProvider.removeCar(snapshot.data[index]);
                          carProvider.getCar();
                        },
                      )
                    ],
                  ),
                );
              else
                return Center(
                  child: Text('Something is wrong'),
                );
            },
          ),
        ),

2 个答案:

答案 0 :(得分:1)

FutureBuilder没有与方法“绑定”。这意味着再次调用carProvider.getCar()绝对不会影响FutureBuilder。

它所做的只是再次调用该函数。

onRefresh: () async {
            return await carProvider.getCar();
          },

一种更简单的方法是只调用setState来重建FutureBuilder。

onRefresh: () async {
            setState((){});
   },

答案 1 :(得分:0)

您可以执行以下操作:

Future refresh() async{
return await carProvider.getCar();
}

然后:

RefreshIndicator(onRefresh: refresh, child: FutureBuilder(future: refresh, ....))