http请求调用api接口时报错

来源:9-3 异步:Future与FutureBuilder实用技巧【好好学,实战经常用】

给我点阳光就灿烂

2019-07-27

调用自己的api接口时报错

class _DashboardPageState extends State<DashboardPage> {
  String showResult = '';
  Future<TopicModel> fetchGet() async {
    final response = await http.get('http://localhost:6340/topic/get/all?page=1&limit=3');
    final result = json.decode(response.body);
    print(response.statusCode);
    print(result["code"]);
    print(result["msg"]);
    return TopicModel.fromJson(result);
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 10.0,
      ),
      body: Column(
        children: <Widget>[
          InkWell(
            onTap: () {
              fetchGet().then((TopicModel value) {
                setState(() {
                  showResult = '结果:${value.title}';
                });
              });
            },
            child: Text("点我"),
          ),
          Text(showResult)
        ],
      ),
    );
  }
}

postman 可以正常返回数据

{
    "code": 1,
    "data": [
        {
            "Key": "1563194626",
            "Title": "爱情",
            "Description": "有些人浅薄,有些人金玉其外败絮其中,但总有一天你会遇见一个彩虹般绚丽的人,当你遇到这个人后,便会觉得其他人只是浮云而已。",
            "QuestionNumber": 0,
            "FocusNumber": 1,
            "Avatar": "http://localhost:8080/topic/1563194626.jpg",
            "Id": "5d2c7502e5f18a2c6d8f192e",
            "Creator": {
                "Id": "5d2aa98fc2f504fea8778176",
                "Avatar": "https://wx.qlogo.cn/mmopen/vi_32/5Tz4wrNl2xPJOiaozUwMXlPqdBL48NicaByLiawynqicOUcPJ2vNJ5twhZ1XSogxGZVuYW8Py1yRUMEkCvbOicSMDPA/132",
                "Nickname": "给我点阳光就灿烂"
            },
            "CreateTime": "2019年 7月 16日"
        },
        {
            "Key": "1563194705",
            "Title": "生活",
            "Description": "人越向往高处的光明,就越要把根扎进黑暗的地底。——尼采",
            "QuestionNumber": 0,
            "FocusNumber": 1,
            "Avatar": "http://localhost:8080/topic/1563194705.png",
            "Id": "5d2c7551e5f18a2c6d8f192f",
            "Creator": {
                "Id": "5d2aa98fc2f504fea8778176",
                "Avatar": "https://wx.qlogo.cn/mmopen/vi_32/5Tz4wrNl2xPJOiaozUwMXlPqdBL48NicaByLiawynqicOUcPJ2vNJ5twhZ1XSogxGZVuYW8Py1yRUMEkCvbOicSMDPA/132",
                "Nickname": "给我点阳光就灿烂"
            },
            "CreateTime": "2019年 7月 16日"
        },
        {
            "Key": "1563194888",
            "Title": "旅行",
            "Description": "我想带你到处去飞翔,走遍世界去观赏。",
            "QuestionNumber": 0,
            "FocusNumber": 1,
            "Avatar": "http://localhost:8080/topic/1563194888.jpeg",
            "Id": "5d2c7608e5f18a2c6d8f1930",
            "Creator": {
                "Id": "5d2aa98fc2f504fea8778176",
                "Avatar": "https://wx.qlogo.cn/mmopen/vi_32/5Tz4wrNl2xPJOiaozUwMXlPqdBL48NicaByLiawynqicOUcPJ2vNJ5twhZ1XSogxGZVuYW8Py1yRUMEkCvbOicSMDPA/132",
                "Nickname": "给我点阳光就灿烂"
            },
            "CreateTime": "2019年 7月 16日"
        }
    ],
    "error": null,
    "msg": "查询成功"
}

图片描述

写回答

1回答

CrazyCodeBoy

2019-07-29

后来问题解决了吗?

0
4
CrazyCodeBoy
回复
给我点阳光就灿烂
其他接口地址能正常请求吗
2019-08-01
共4条回复

Flutter从入门到进阶 实战携程网App 一网打尽核心技术

解锁Flutter开发新姿势,,系统掌握Flutter开发核心技术。

4788 学习 · 3270 问题

查看课程