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
var streetsFromJson = parsedJson['streets'];
List<String> streetsList = new List<String>.from(streetsFromJson);
Thnx for https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51
Just pay attention to the error
List<dynamic> is not a subtype of type Map<String, dynamic>
basically it means the data which you are receiving after hitting a webservice is in form of List but inside data class which you have used is of type Map .So, it is unable to parse the data due to structure mismatch as Map is defined in instead of List in data class.
Recheck your Json response and corresponding PODO class
API returns JSON array not json object so that is List not Map.
i.e. User json is first element of Json Array.
So to get first element use first index. Inside fetch Info update
return Users.fromJson(jsonresponse[0]);
Use
List<dynamic> output = jsonDecode(yourJsonSource);
instead of
Map<String, dynamic> output = jsonDecode(yourJsonSource);
Check out this Answer
Future<List<ProductList>> 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<List<ProductList>>(
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: <Widget>[
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