0

I'm querying get into flutter, and I'm getting back array (one object in the array). I want to parse this answer and assign the data from the array object to the variable. But I can't do it. Here is my request:

  Future<Null>   example(
      String? task_id
      ) async {

    HttpClient client = new HttpClient();
    client.badCertificateCallback =
    ((X509Certificate cert, String host, int port) => true);

    final String url = urlVar "?id="+  '$task_id';

    final request = await client
        .getUrl(Uri.parse(url))
        .timeout(Duration(seconds: 5));


    HttpClientResponse response = await request.close();

    var responseBody = await response.transform(utf8.decoder).join();

    Map jsonResponse = json.decode(responseBody);

    print(jsonResponse);


  }

My answer json

[
  {
    "id": "d290111e-6c54-4b01-90e6-d701748f0851",
    "name": "Parse example",
   }
]

I want to parse this answer and assign it to my variables. I did it like when the answer was in the object, I did it like this. But with array not, I will be grateful for help)

    var responseBody = await response.transform(utf8.decoder).join();

    Map jsonResponse = json.decode(responseBody);

    print(jsonResponse);
    if (response.statusCode == 200) {
      global.taskId = jsonResponse['id'] ;
      global.taskName = jsonResponse['name'];

    }
Andrii Havrylyak
  • 675
  • 6
  • 16

1 Answers1

0

Try this

if (response.statusCode == 200) {
  global.taskId = jsonResponse[0]['id'] ;
  global.taskName = jsonResponse[0]['name'];

}
Ananda Pramono
  • 899
  • 1
  • 6
  • 18