Taking specific data from a "(Response.body)"

Asked

Viewed 482 times

2

I have that code:

class _EstabelecimentosPageState extends State<EstabelecimentosPage> {
  final String url = codeUrl;

@override void 
 initState() {
  super.initState();
  this.getJsonData();
 }

 Future<String> getJsonData() async {
  var response = await http.get(url);
  print("return " + response.body);
  setState(() {
   var convertDataToJson = json.decode(response.body);
   var data = convertDataToJson['data']['codigo'];
   return data;
  });

 return "Success";
 }
}

That returns a

{"status":"success","data":{"codigo":"código"}}

I need to only take the "code" within the "date", but I’m not getting, I appreciate any help, I’m beginner and it’s giving me the nerve because I think it’s a simple mistake, but I can’t find.

1 answer

4


I got:

Future<String> getJsonData() async {
 var response = await http.get(url);
 if (response.statusCode == 200) {
   var convertDataToJson = json.decode(response.body);
   var data = convertDataToJson['data']['codigo'];
   print(data);
   return data;
 } else {
   throw ('error ${response.statusCode}');
 }
}

Browser other questions tagged

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