Flutter: How am I supposed to access the response body in this Delete request?

Viewed 1099

I'm using this piece of code to make a delete request with a body :

Future deleteAcc(data) async {
    Map<String, String> headers = {
      "Content-Type": "application/json",
      'Authorization': '$token'
    };

    print('In Provider');
    final url = Uri.parse('$baseUrl$deleteUser');
    final req = http.Request("DELETE", url);
    req.headers.addAll(headers);
    req.body = jsonEncode(data);
    final resp = await req.send();
    return await resp.stream.bytesToString();
}

in function call I want to access the response message, on usual I access it like this:

await _bloc.deleteAcc(data).then((value) {
                    print(value.message);
                  });

But when I use it in this request I get this error message:

E/flutter ( 5868): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: NoSuchMethodError: Class 'String' has no instance getter 'message'.
E/flutter ( 5868): Receiver: "{\"code\":711,\"message\":\"list removed successfully\"}"
E/flutter ( 5868): Tried calling: message

Any help to deal with this please?

1 Answers

You should decode the received json:

Map<String, dynamic> temp = json.decode(value);

then you can access the message:

String message = temp['message'];
Related