在Flatter(Mysql Localhost)中从Node.js rest api获取数据时出错
错误:发生异常。 _TypeError(类型“List”不是类型“Map”的子类型)在Flatter(Mysql Localhost)中从Node.js rest api获取数据时出错,mysql,node.js,api,flutter,Mysql,Node.js,Api,Flutter,错误:发生异常。 _TypeError(类型“List”不是类型“Map”的子类型) Future您的响应是对象列表,而不是单个对象。因此,您必须传递列表的[0]索引 尝试下面的代码,它将为您工作 Future<Album> fetchAlbum() async{ final response = await http.get(Uri.encodeFull("192.168.43.106:8080/customers/"),
Future您的响应是对象列表,而不是单个对象。因此,您必须传递列表的[0]索引
尝试下面的代码,它将为您工作
Future<Album> fetchAlbum() async{
final response = await http.get(Uri.encodeFull("192.168.43.106:8080/customers/"),
headers: { "Accept": "application/json" } );
if (response.statusCode == 200) {
var str = jsonDecode(response.body);
return Album.fromJson(str[0]);
}
else{
throw Exception('Failed to load Album');
}
}
class Album {
int id;
String email;
String name;
Album({this.id, this.email, this.name});
Album.fromJson(json)
: id = json['id'],
email = json['email'].toString(),
name = json['name'].toString();
}
Future fetchAlbum()异步{
最终响应=等待http.get(Uri.encodeFull(“192.168.43.106:8080/customers/”),
标题:{“接受”:“应用程序/json”});
如果(response.statusCode==200){
var str=jsonDecode(response.body);
return Album.fromJson(str[0]);
}
否则{
抛出异常(“加载相册失败”);
}
}
班级相册{
int-id;
字符串电子邮件;
字符串名;
相册({this.id,this.email,this.name});
fromJson(json)
:id=json['id'],
email=json['email'].toString(),
name=json['name'].toString();
}
将您尝试过的代码放在这里,而不是图片,并显示实际错误(未来),标题:{“接受”:“应用程序/json”});如果(response.statusCode==200){return Album.fromJson(json.decode(response.body));}或者{throw Exception('Failed to load Album');}您也可以共享相册模型代码,并且来自api的响应?@MuhammadNoman上面是更新的代码。请帮助完成代码链接:
Future<Album> fetchAlbum() async{
final response = await http.get(Uri.encodeFull("192.168.43.106:8080/customers/"),
headers: { "Accept": "application/json" } );
if (response.statusCode == 200) {
var str = jsonDecode(response.body);
return Album.fromJson(str[0]);
}
else{
throw Exception('Failed to load Album');
}
}
class Album {
int id;
String email;
String name;
Album({this.id, this.email, this.name});
Album.fromJson(json)
: id = json['id'],
email = json['email'].toString(),
name = json['name'].toString();
}