在 Flutter 中从 Node.js rest api 获取数据时出错(Mysql Localhost)

埃巴杜拉·阿米尼

错误:发生异常。_TypeError(“List”类型不是“Map”类型的子类型)

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) {
        return Album.fromJson(json.decode(response.body));
    }
    else{
        throw Exception('Failed to load Album');
    }
}
class  Album {
final int id;
final String email;
final String name;

Album({this.id, this.email, this.name});

factory Album.fromJson(Map<String, dynamic> json){
  return Album(
    id: json['id'],
    email: json['email'],
    name: json['name'],
  );
}

}

在此处输入图片说明

穆罕默德·诺曼

您的响应是对象列表,而不是单个对象。因此,您必须传递列表的 [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();

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Node.js REST API从MongoDB中获取数据

在flutter中从api获取数据时出错

在使用node.js中的REST API时请求参数

如何使用嵌套循环从flutter中的rest api获取数据?

在 Node js 中 Rest Api 请求(curl 到 Node.js,python 到 Node.js)

需要在Slack App Node.js中的异步功能内获取REST API

node.js 中的 Rest api 调用问题

Node.js中Rest API的Forever Forever模块

REST API node.js

使用 Node Js 和 express 调用来自 rest api 的 post 请求时出错。请求数据变空

如何从Rest API获取数据并在angular js中显示

Flutter Rest API获取JSON

在rest api monngodb node express中过滤数据

从 Javascript/Node 中的异步数据创建 REST API

使用node.js在MySQL中插入大容量数据时出错(错误代码:'ECONNRESET')

如何从 REST API 获取数据到 Lex ChatBot (Node.js)

从 Rest api 异步获取数据并使用 node.js 将它们加载到 mongodb - 使用承诺

使用Node.js Express的Rest API

简单的Node.JS REST API调用

在Flutter中重置密码Rest Api

从reactjs中的API获取数据时出错

如何让用户只能看到自己的数据,node js rest api with mysql

如何在 Ember JS 中获取 REST Api?

如何保护开放的 REST API 免受 Node.js 中的滥用?

REST API单元测试,在Node.js中返回承诺

是否可以在Node.js中运行Spring Boot REST API

使用 express 和 ES6 类在 node js rest api 中查找路由的问题

使用JWT(Node.js + Mongoose)在REST API中管理用户权限的最佳方法

使用setInterval调用rest api并在node.js中承诺