1

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;
  }
Firaun
  • 369
  • 1
  • 5
  • 21
  • create a bool obs variable.`var gettingDataCompleted = false.obs` and inside builder check it for rebuild. – Z-Soroush May 01 '21 at 23:00

2 Answers2

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
});
4xMafole
  • 479
  • 1
  • 8
  • 20
Dwi Nur Rohman
  • 425
  • 2
  • 5
  • 13
0

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

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77