Flutter layout style change according to api response

Issue

I want to change my layout according to an api response. For example if the api response return gridview I want to display my data in gridview and If it return horizontal listview then i want to have my data in listview format.

So my question is which is the best way to implement? I have tried listview builder but it is not working.

I will have my layout style in my api response.

Solution

Use flutter’s Futurebuilder for this.

Future builder will build the widget according to the Api response.
Here is an example of the Future builder.

return FutureBuilder(
  future: apiCall(),
  builder: (context, response){
    if(response.hasData){
      var data = response.data;
      if(data["layout"] == "gridview") return GridView.builder();
      else return ListView.builder();
    }else return Container();
  },
);

In this way you will be able to build your layout according to API response.

and as always..
Happy Coding

Answered By – Abhay Kumar

Answer Checked By – Candace Johnson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.