Home > database >  How to refresh future builder data in flutter
How to refresh future builder data in flutter

Time:08-30

I want to refresh future builder data when I click on the button. What can I do to add this functionality to my app?

I want to click on the categories and want to display the data related to the categories I clicked on.

I tried it with stream builder but it did not work. if you know how to make it work with future builders or stream builders then please provide so solution to it.

enter image description here

   SizedBox(
                height: 60,
                child: ListView.builder(
                  shrinkWrap: true,
                  scrollDirection: Axis.horizontal,
                  itemCount: categories.length,
                  itemBuilder: (BuildContext context, index) {
                    return Center(
                      child: InkWell(
                        onTap: () {
                          () {};
                          setState(() {});
                        },
                        child: Container(
                       //  ...rest code...
                            child: Text(
                              categories[index]['category_name'].toString(),
                              style: const TextStyle(color: Colors.white),
                            ),
                          ),
                        ),
                      ),
                    );
                  },
                ),
              ),
              FutureBuilder(
                future: getProductsByCategory(),
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                  if (snapshot.hasData) {
                    var data = snapshot.data;
                    return GridView.builder(
                    ...rest code
                    );
                  } else {
                    return GridView.builder(
                      itemCount: 7,
                      shrinkWrap: true,
                      physics: const NeverScrollableScrollPhysics(),
                      gridDelegate:
                         ... rest code
                      },
                    );
                  }
                },
              ),

CodePudding user response:

When you click on categories then save a value in global variable like-

String categoryName = "your_category_name";

then while calling the list of data set condition like -

if(categoryName.compareTo("your_listData_category")==0){
   //showListData in gridView
}

CodePudding user response:

SizedBox(
                height: 60,
                child: ListView.builder(
                  shrinkWrap: true,
                  scrollDirection: Axis.horizontal,
                  itemCount: categories.length,
                  itemBuilder: (BuildContext context, index) {
                    return Center(
                      child: InkWell(
                        onTap: () {
                          //Call this when Clicked Store in one List And Display Direct
                          List data = await getProductsByCategory()
                          
                        },
                        child: Container(
                       //  ...rest code...
                            child: Text(
                              categories[index]['category_name'].toString(),
                              style: const TextStyle(color: Colors.white),
                            ),
                          ),
                        ),
                      ),
                    );
                  },
                ),
              ),
//Use Direct Gridview
             GridView.builder(
                    ...rest code
                    )

After complete Api Call change state

GridView.builder you can use to show Progress

List == null ? ProgressBar : Restcode

  • Related