0
votes

Im trying to get json data from the server

here is the code:

 void main() async{
      List data = await getData();
      print(data);
      runApp(MyApp());
    }
    
    Future<List> getData() async {
      String myUrl = "https://dashboard.ssitanas.com/public/api/categories";
      http.Response response = await http.get(myUrl, headers: {
        'Accept': 'application/json',
      });
      return json.decode(response.body);
    
    }

what is the problem ?

1
add your JSON response. - xbadal
{ "success": true, "data": [ { "id": 5, "Category_name": "D" }, { "id": 6, "Category_name": "E" }, { "id": 7, "Category_name": "F" } ], "message": "all category sent" } - anasalhajhasan

1 Answers

0
votes

The response coming from the api is a Map, not a List, but from the looks of things, there seems to be a list inside the map

so just do this :

    var res = json.decode(response.body);
    var listData = res["data"]; 
    //assuming the list inside the map is called data
    return listData;