2

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?

julemand101
  • 28,470
  • 5
  • 52
  • 48
Shero_Ha
  • 41
  • 2
  • 4

1 Answers1

1

You should decode the received json:

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

then you can access the message:

String message = temp['message'];
Abbasihsn
  • 2,075
  • 1
  • 7
  • 17