【发布时间】:2023-04-06 19:10:01
【问题描述】:
我正在尝试在 Flutter 中创建 API 请求,但我收到以下错误作为响应
类型 'List
' 不是类型 'Map ' 的子类型
我正在尝试创建第一个 API,如果方法合适,请告诉我
这是我的代码
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}");
}
这是我的示例 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
}
]
【问题讨论】: