带有状态已满的窗口小部件的Flutter Listview构建器不会随着父窗口小部件中的设置状态更新

时间:2020-05-30 14:21:22

标签: flutter statefulwidget

我使用状态小部件创建的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
  }
});
}

initstate =>

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

}

body =>

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),
                        });
                      }
                    });
                  },
                );
              },
            );
        },
      ),
    ),
  ),

0 个答案:

没有答案