0

The listviewbuilder I created with stateful widgets only updates when I scroll it.When I used stateless widgets it reloads perfectly, but it doesn't work with stateful, I have a RefreshIndicator which calls the data getting method, I don't know whats wrong, can anyone help please?

My data retrieving method =>

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),
                        });
                      }
                    });
                  },
                );
              },
            );
        },
      ),
    ),
  ),
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Please provide your code where you encounter the problem – Rustem Kakimov May 30 '20 at 14:23
  • 1
    I edited the question and provided the code. sorry – CHANDUKA SAMARASINGHE. May 30 '20 at 15:08
  • No worries. So your problem is that items do not update after you call `getData`? Can you provide the code inside `itemBuilder`? – Rustem Kakimov May 30 '20 at 15:16
  • I can't reproduce the issue. If you still need help, you can upload your project to github repository and share it here. I will take a look. – Rustem Kakimov May 30 '20 at 16:24
  • Should I use didupdatewidget in the "DataBox" widget when the RefreshIndicator is clicked ? I checked and It loads a new list to the data list but it doesnt update the list. BUt when I scrolldown and come up the changes are happend and the new list is loaded.This doesnt happen when I use a stateless widget as the DataBox widget. – CHANDUKA SAMARASINGHE. May 31 '20 at 03:16

0 Answers0