List on the Query screen the REST API , JSON on Flutter

Asked

Viewed 694 times

1

I’m having trouble dealing with the return of a REST request.

API: Rails is working properly.

my job

 Future <Map> _getProdPromo() async {
    http.Response response = await http.get("http://10.0.2.2:3000/infoPromocao.json");
    List responseJson = json.decode(response.body);
    return json.decode(response.body);
  }

To test Console Print.

@override
  void initState() {
    super.initState();
    _getProdPromo().then((map){
      print(map);
    });
  }

Error:

flutter (19914): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'List' is not a subtype of type 'Futureor>' E/flutter (19914): #0
_Productsfromocionaisstate. _getProdPromo (package:gestoque/products_promotion_Dart:19:5) E/flutter (19914):

I make the Query via Advanced Rest and the following data is returned:

[
    {
        "ad_codprodint": "40011333ME",
        "codprod": 1632,
        "descrprod": "BETONEIRA 400L PRIME MONOF 110V MENEGOT",
        "vlrvenda": 2850
    },
    {
        "ad_codprodint": "40011333ME",
        "codprod": 1632,
        "descrprod": "BETONEIRA 400L PRIME MONOF 110V MENEGOT",
        "vlrvenda": 3130
    },
]
  • Debug your code and tell us when the error occurs and if possible show some example of the return of your API.

  • Edit your question with this JSON return, let’s try to get everything organized :D Just click there on the button Edit just above the comments and add the information in your question

1 answer

1


Your problem is you get one Jsonarray and tries to treat it as a simple Map<String, dynamic>...

In case you first need to do your code JSON received, thus having in hand a List<Map<String, dynamic>, There yes you can catch the objects doing

  Map<String, dynamic> objeto = minhaListaRetornada[0]

I created an example in Dartpad where you can see the thing working:

import "dart:convert";

void main() {

String json = '[{"ad_codprodint": "40011333ME","codprod": 1632,"descrprod": "BETONEIRA 400L PRIME MONOF 110V MENEGOT","vlrvenda": 2850},{"ad_codprodint": "40011333ME","codprod": 1632,"descrprod": "BETONEIRA 400L PRIME MONOF 110V MENEGOT","vlrvenda": 3130}]';


 Future <List<dynamic>> _getProdPromo() async {
   final result = jsonDecode(json); 

   return result;
  }

_getProdPromo().then((map){
      print(map[1]);
});

}

I wrote an article on Medium that may be useful to you Uncomplicating JSON in Flutter

  • 1

    Show Ball, I managed to solve the problem by doing exactly that flw me. Very obg, I will read the article

Browser other questions tagged

You are not signed in. Login or sign up in order to post.