I am currently building an app to read data through an api and I am trying to parse a JSON api from JSON Placeholder.
I made a model class for the Users (users_futur
Check out this Answer
Future> getProductList() async {
print("comes");
String productURl= mainURL+'api/store/product-with-category/';
final response = await http.get(productURl,headers:{"Content-Type":
"application/json"});
List jsonResponse = json.decode(response.body);
return jsonResponse.map((job) => new ProductList.fromJson(job)).toList();
}
Fetch function
FutureBuilder>(
future: getProductList(),
builder: (context, snapshot) {
print("snapshot");
print(snapshot.data);
if (snapshot.hasData) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: GridView.builder(
itemCount: snapshot.data.length,
gridDelegate:SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 2,),
itemBuilder: (BuildContext context, int i){
return Card(
child: Container(
decoration: BoxDecoration(
border: Border.all(width: 0.5,color: Colors.grey)
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
text18(snapshot.data[i].title, Colors.black, FontWeight.bold)
],
),
),
),
);
}
)
);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
// By default, show a loading spinner.
return CircularProgressIndicator();
},
),
just change URL and create model class