I am trying to create a API request in Flutter but i am getting following error as response
type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
I am trying to create first API and kindly let me know if the approach is fine
here is my code
import 'package:flutter/material.dart';
class Product {
final int id;
final String title, description;
final String images;
final List<Color> colors;
final double price;
final double rating;
final bool isFavourite, isPopular;
Product(
{this.id,
this.images,
this.colors,
this.title,
this.price,
this.rating,
this.description,
this.isFavourite,
this.isPopular});
factory Product.fromJson(Map<String, dynamic> json) {
return Product(
id: json['id'],
images: json['images'],
title: json['title'],
price: json['price'],
rating: json['rating'],
description: json['description'],
);
}
}
Future<Product> fetchProd() async {
final response = await http.get('https://test.com/sampleapi.php');
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
return Product.fromJson(jsonDecode(response.body));
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load album');
}
}
class ProdList extends StatefulWidget {
@override
_ProdListState createState() => _ProdListState();
}
class _ProdListState extends State<ProdList> {
Future<Product> futureProdLists;
@override
void initState() {
super.initState();
futureProdLists = fetchProd();
}
@override
Widget build(BuildContext context) {
return Column(children: [
Padding(
padding:
EdgeInsets.symmetric(horizontal: getProportionateScreenWidth(20)),
child: SectionTitle(title: "Popular Products", press: () {}),
),
SizedBox(height: getProportionateScreenWidth(20)),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: FutureBuilder<Product>(
future: futureProdLists,
builder: (context, snapshot) {
print(snapshot);
if (snapshot.hasData) {
return Text(snapshot.data.title);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
Here is my sample API
[
{
"id": "0001",
"images": "assets/images/ps4_console_white_1.png",
"title": "Wireless Controller for PS4",
"price": 25,
"description": "description",
"rating": 4.8,
"rating (copy)": 4.8,
"isFavourite": true,
"isPopular": true
},
{
"id": "0001",
"images": "assets/images/ps4_console_white_1.png",
"title": "Wireless Controller for PS4",
"price": 25,
"description": "description",
"rating": 4.8,
"rating (copy)": 4.8,
"isFavourite": true,
"isPopular": true
},
{
"id": "0001",
"images": "assets/images/ps4_console_white_1.png",
"title": "Wireless Controller for PS4",
"price": 25,
"description": "description",
"rating": 4.8,
"rating (copy)": 4.8,
"isFavourite": true,
"isPopular": true
},
{
"id": "0001",
"images": "assets/images/ps4_console_white_1.png",
"title": "Wireless Controller for PS4",
"price": 25,
"description": "description",
"rating": 4.8,
"rating (copy)": 4.8,
"isFavourite": true,
"isPopular": true
},
{
"id": "0001",
"images": "assets/images/ps4_console_white_1.png",
"title": "Wireless Controller for PS4",
"price": 25,
"description": "description",
"rating": 4.8,
"rating (copy)": 4.8,
"isFavourite": true,
"isPopular": true
}
]