Flutter 类型';列表<;动态>';不是类型为';地图<;字符串,颤振应用程序中的动态
我试图在颤振中创建一个API请求,但得到以下错误作为响应 类型“List”不是类型“Map”的子类型 我正在尝试创建第一个API,请告诉我该方法是否合适 这是我的密码Flutter 类型';列表<;动态>';不是类型为';地图<;字符串,颤振应用程序中的动态,flutter,dart,Flutter,Dart,我试图在颤振中创建一个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;
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
响应提供了产品对象的json
数组,您正试图将该数组转换为产品对象,这就是问题所在
现在,您必须迭代json
数组,将每个元素转换为产品对象,并将它们存储在列表中
用下面的代码片段替换fetchProd
方法
Future<List<Product>> fetchProd() async {
List<Product> prodList = [];
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.
var jsonList = jsonDecode(response.body);
for(var prod in jsonList){
prodList.add(Product.fromJson(prod));
}
return prodList;
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load album');
}
}
Future fetchProd()异步{
列表prodList=[];
最终响应=等待http.get('https://test.com/sampleapi.php');
如果(response.statusCode==200){
//如果服务器确实返回了200 OK响应,
//然后解析JSON。
var jsonList=jsonDecode(response.body);
for(jsonList中的var prod){
add(Product.fromJson(prod));
}
返回产品列表;
}否则{
//如果服务器没有返回200 OK响应,
//然后抛出一个异常。
抛出异常(“加载相册失败”);
}
}
问题是您的api
响应提供了产品对象的json
数组,您正试图将该数组转换为产品对象,这就是问题所在
现在,您必须迭代json
数组,将每个元素转换为产品对象,并将它们存储在列表中
用下面的代码片段替换fetchProd
方法
Future<List<Product>> fetchProd() async {
List<Product> prodList = [];
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.
var jsonList = jsonDecode(response.body);
for(var prod in jsonList){
prodList.add(Product.fromJson(prod));
}
return prodList;
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load album');
}
}
Future fetchProd()异步{
列表prodList=[];
最终响应=等待http.get('https://test.com/sampleapi.php');
如果(response.statusCode==200){
//如果服务器确实返回了200 OK响应,
//然后解析JSON。
var jsonList=jsonDecode(response.body);
for(jsonList中的var prod){
add(Product.fromJson(prod));
}
返回产品列表;
}否则{
//如果服务器没有返回200 OK响应,
//然后抛出一个异常。
抛出异常(“加载相册失败”);
}
}
试试这个
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 (response.body as List)
.map((item) => Product.fromJson(item))
.toList();
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load album');
}
}
Future fetchProd()异步{
最终响应=等待http.get('https://test.com/sampleapi.php');
如果(response.statusCode==200){
//如果服务器确实返回了200 OK响应,
//然后解析JSON。
返回(response.body作为列表)
.map((项)=>Product.fromJson(项))
.toList();
}否则{
//如果服务器没有返回200 OK响应,
//然后抛出一个异常。
抛出异常(“加载相册失败”);
}
}
试试这个
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 (response.body as List)
.map((item) => Product.fromJson(item))
.toList();
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load album');
}
}
Future fetchProd()异步{
最终响应=等待http.get('https://test.com/sampleapi.php');
如果(response.statusCode==200){
//如果服务器确实返回了200 OK响应,
//然后解析JSON。
返回(response.body作为列表)
.map((项)=>Product.fromJson(项))
.toList();
}否则{
//如果服务器没有返回200 OK响应,
//然后抛出一个异常。
抛出异常(“加载相册失败”);
}
}
这肯定会奏效。
final Product=productFromJson(jsonString)代码>
导入“dart:convert”;
List productFromJson(String str)=>List.from(json.decode(str.map)(x)=>Product.fromJson(x));
字符串productToJson(List data)=>json.encode(List.from(data.map)((x)=>x.toJson());
类产品{
产品({
这个身份证,
这是一张照片,
这个名字,
这个价格,,
这个.说明,,
这个.评级,,
这是一份复印件,
这是我的最爱,
这是流行的,
});
字符串id;
字符串图像;
字符串标题;
国际价格;
字符串描述;
双重评级;
双比例复印;
布尔是我的最爱;
布尔是流行的;
factory Product.fromJson(映射json)=>Product(
id:json[“id”],
图像:json[“图像”],
标题:json[“标题”],
价格:json[“价格”],
description:json[“description”],
评级:json[“评级”].toDouble(),
ratingCopy:json[“评级(副本)”]。toDouble(),
isfavorite:json[“isfavorite”],
isPopular:json[“isPopular”],
);
映射到JSON()=>{
“id”:id,
“图像”:图像,
“头衔”:头衔,
“价格”:价格,
“描述”:描述,
“评级”:评级,
“评级(副本)”:评级副本,
“IsFavorite”:IsFavorite,
“isPopular”:isPopular,
};
}
这肯定会奏效。
final Product=productFromJson(jsonString)代码>
导入“dart:convert”;
List productFromJson(String str)=>List.from(json.decode(str.map)(x)=>Product.fromJson(x));
字符串productToJson(List data)=>json.encode(List.from(data.map)((x)=>x.toJson());
类产品{
产品({
这个身份证,
这是一张照片,
这个名字,
这个价格,,
这个.说明,,
这个.评级,,
这是一份复印件,
这是我的最爱,
这是流行的,
});
字符串id;
字符串图像;
字符串标题;
国际价格;
字符串描述;
双重评级;
双比例复印;
布尔是我的最爱;
布尔是流行的;
factory Product.fromJson(映射json)=>Product(
id:json[“id”],
图像:json[“图像”],
标题:json[“标题”],
价格:json[“价格”],
description:json[“description”],
评级:json[“评级”].toDouble(),
ratingCopy:json[“评级(副本)”]。toDouble(),
isfavorite:json[“isfavorite”],
isPopular:json[“isPopular”],
);
映射到JSON()=>{
“id”:id,
“图像”:图像,
“头衔”:头衔,
“价格”:价格,
“描述”:描述,
“评级”:评级,
“评级(副本)”:评级副本,
“IsFavorite”:IsFavorite,
“isPop