从api检索数据时出现此错误? [英] I got this error while retrieving data from the api?

查看:52
本文介绍了从api检索数据时出现此错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码中的错误是什么?在控制台中,出现这样的错误

what is the error in this code? In console, I got error like this

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)]未处理的异常: 类型"_InternalLinkedHashMap"不是以下类型的子类型 输入可迭代

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type '_InternalLinkedHashMap' is not a subtype of type 'Iterable

class _MytimeoffState extends State<Mytimeoff> {

  List<Map> list = [];

  void getList() async {
    var data = await http
        .get('https://my-json-server.typicode.com/darshanaAlex/db2/time_off');
    var jsonData = json.decode(data.body);

    setState(() {
      for (Map js in jsonData) {
        list.add(js);
      }
    });
    print(jsonData);
  }

  @override
  void initState() {
    super.initState();
    getList();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold();
}
}

推荐答案

响应为Map 它在离开类型处包含List

The response is a Map and it contains List at leavetypes !

但是您正在尝试将直接地图访问到列表中

But you are trying to access direct map into list

class _MytimeoffState extends State<Mytimeoff> {

  List<Map> list = [];
  Map leaveRoot ={};

  void getList() async {
    var data = await http
        .get('https://my-json-server.typicode.com/darshanaAlex/db2/time_off');
    leaveRoot = Map.from(json.decode(data.body));

    setState(() {
      for (Map js in leaveRoot['leavetype']) {
        list.add(js);
      }
    });
    print(jsonData);
  }

  @override
  void initState() {
    super.initState();
    getList();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold();
}
}

这篇关于从api检索数据时出现此错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆