0
votes

This is the JSON data, i want to retrive

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

[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();
}
}
1

1 Answers

1
votes

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();
}
}