如何在Futurebuilder中的ListView上使用Refreshindicator?

时间:2020-03-05 14:25:17

标签: firebase flutter

我无法重新加载作为Futurebuilder的子代的列表。

我目前正在使用来自futureprovider的数据,我知道这可能是不必要的,但我想学习并将其保留在此处。 (这有问题吗?)

如您所见,我有2个ListView,一个嵌套在另一个中。我想将顶级ListView包装在RefreshIndicator中,以从Firebase加载新数据,但是我不知道如何以及在何处更新数据。

class _DayTransferListState extends State<DayTransferList> {

  @override
  Widget build(BuildContext context) {
    final days = Provider.of<List<Day>>(context);        //<-- i am populating my list with this data atm
    return FutureBuilder(
      future: DatabaseService().dayTransferData(),      //<-- dayTransferData returns a Future<List<Day>>
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
            //...
          default:
            if (snapshot.hasError) {
              return Text('haserror');
            } else {
              return RefreshIndicator(                    //<-- refreshindicator
                onRefresh: () async {
                  _refreshItems;
                },
                child: ListView.builder(
                  physics: BouncingScrollPhysics(),
                  itemCount: days.length,
                  itemBuilder: (
                    BuildContext context,
                    int dayindex,
                  ) {
                    return Column(
                      children: <Widget>[
                        //...
                        Container(
                          width: double.infinity,
                          padding: EdgeInsets.all(10),
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(kButtonRadius),
                          ),
                          child: ListView.builder(                          //<-- second, nested ListView
                            physics: ClampingScrollPhysics(),
                            shrinkWrap: true,
                            itemCount: days[dayindex].transfers.length,
                            itemBuilder: (BuildContext context, int index) {
                              return TransactionTile(
                                transactionIndex: index,
                                dayIndex: dayindex,
                                transfer: days[dayindex].transfers[index],
                              );
                            },
                          ),
                        ),
                      ],
                    );
                  },
                ),
              );
            }
        }
      },
    );
  }

  Future _refreshItems() async {                                       //<-- method called in "onRefresh"
    await DatabaseService().dayTransferData();          //<-- dayTransferData returns a Future<List<Day>>
    setState(() {});
    return null;
  }
}

1 个答案:

答案 0 :(得分:0)

它不适用于建造者。做

ListView(
  children: List.generate(/*...*/),
)