0

I am using GetX and FutureBuilder to build a list of Cards from my DB. Lets say I have 10 products in DB then 10 cards are shown. When I add one more product, the Cards on the HomeScreen aren't updated and I have to Navigate in and Out of page to show the 11th product.

How can I force the update i.e. probably make a "Refresh" button that may load the latest data.

PS: I do not want to use STREAM-BUILDER as I don't wish to listen to all changes actively but only when needed. I also cannot use SetState() as I am using GetX hence no StatefulWidget.

Here is my Card class:

FutureBuilder(
                future: databaseController.getData()
                builder: (context, snapshot) {
                   return StaggeredGridView.countBuilder(
                      itemCount: snapshot.data.length,
                      crossAxisCount: 2,
                      itemBuilder: (BuildContext context, int index) =>
                          GetX<FindDeviceLocation>(builder: (controller) {
                        return CreateTheCard(
                            lindex: index,
                            location: snapshot.data[index]["location"],
                            summary: snapshot.data[index]["summary"],
                            description: snapshot.data[index]["description"],
                            category: snapshot.data[index]["category"],
                            imageURL: snapshot.data[index]["adImage"],
                            
                            onTapFunction: () => Get.to(DetailPage(
                                  post: snapshot.data[index],
                                )));
                      }),

This is my method that fetches data from DB:

Future getData() async {
    QuerySnapshot _firebaseDb = await FirebaseFirestore.instance
        .collection("items")
        .where("status", isEqualTo: true)
        .orderBy("postTime", descending: true)
        .get();
          
    return _firebaseDb.docs;
  }
1
  • create a bool obs variable.var gettingDataCompleted = false.obs and inside builder check it for rebuild. May 1, 2021 at 23:00

2 Answers 2

0

The databaseController has a method call update(),so you can call databaseController.update() when you need to update your data.

0

Use the getx worker Ever: If data change you can update the view

ever(index, (value) {
// call your function here
// any code will be called any time index changes
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.