0

我使用有状态小部件创建的 listviewbuilder 仅在滚动时更新。当我使用无状态小部件时,它会完美重新加载,但它不适用于有状态,我有一个调用数据获取方法的 RefreshIndicator,我不知道出了什么问题,有人可以帮忙吗?

我的数据检索方法 =>

List<DataModelWidget> data= [];

Future<void> getData() async {

QuerySnapshot current = await _firestore.collection('x').getDocuments();
int max = current.documents[0].data['id'];
int start = Random().nextInt(max);
int end = start + 100;
data= [];

QuerySnapshot x = await _firestore
    .collection('x')
    .where('id', isGreaterThanOrEqualTo: start)
    .where('id', isLessThanOrEqualTo: end)
    .getDocuments();
int i = 0;

setState(() {
  for (var item in x.documents) {
   //adding data to the global data list
  }
});
}

初始化状态 =>

@override
void initState() {
super.initState();
getCurrentUser();
getData();

}

身体=>

body: Container(
    decoration: BoxDecoration(
      image: DecorationImage(
          image: AssetImage('assets/back1.png'), fit: BoxFit.cover),
    ),
    child: RefreshIndicator(
      onRefresh: getData, //also calls this method in the initstate

      child:  ListView.builder(
              itemCount: data.length,
              itemBuilder: (context, index) {
                return DataBox(//my statefull widget
                  widgetData: data[index],
                  handleLike: () {

                    setState(() {
                      if (data[index].liked == false) {
                        data[index].liked = true;
                        data[index].hearts += 1;
                        _firestore
                            .collection('x')
                            .document(x[index].ref)
                            .updateData(
                                {'hearts': FieldValue.increment(1)});
                      } else {
                        data[index].liked = false;
                        data[index].hearts -= 1;
                        _firestore
                            .collection('x')
                            .document(x[index].ref)
                            .updateData({
                          'hearts': FieldValue.increment(-1),
                        });
                      }
                    });
                  },
                );
              },
            );
        },
      ),
    ),
  ),
4

0 回答 0