How do I decode this response body?
1,569
Solution 1
I solved the problem adding this line
String _body = res.body;
as described below,
await http
.post(Uri.encodeFull(mainURL + registrEndPoint + _qr))
.then((res) {
if (res.statusCode == 202) {
String _body = res.body; //<--- HERE!
Map _json = jsonDecode(_body);
String _id = _json['identifier'];
return _id ;
});
Thank you all for the help!
Solution 2
By looking at the dart:convert documentation, you’ll see that jsonDecode() returns a
Map<String, dynamic>
, meaning that you do not know the types of the values until runtime.
Map<String, dynamic> body = jsonDecode(jsonString);
print('Howdy, ${body['identifier']}!');
Author by
Francesco Iapicca
Self taught developer in love with Flutter and Dart
Updated on December 08, 2022Comments
-
Francesco Iapicca over 1 year
I have this response body from a http push
"{"identifier":"00000000-0000-0000-0000-00000000000"}"
I'd like to get the 000000... part as string
that's the relevant part of my code
.
.. async { await http .post(Uri.encodeFull(mainURL + registrEndPoint + _stuff)) .then((res) { if (res.statusCode == 202) { Map _body = jsonDecode(res.body); // I checked debugging, the respons boy is ok String _id =_body['identifier']; return _id; }...
I believe I'm missing something in the 'mapping'
and I suspect the combo 'quote-curlyBraces-quote' defeat my jsonDecode;any suggestion?
thanks in advance
-
Günter Zöchbauer over 5 yearsWhat do you get for
String _id =_body['identifier'];
? -
Francesco Iapicca over 5 yearsappear to be null, when I debug it just skips the breakpoint
-
Günter Zöchbauer over 5 yearsShouldn't be hard to investigate using the debugger what's actually in
_body
. -
Francesco Iapicca over 5 years'hoovering' the mouse on it doesn't show anything, next step is skipped and the return is null
-
-
Francesco Iapicca over 5 yearsthank for the answer, but it seems exactly what I'm already doing [edit: i see the values returned in runtime while debugging, res body value is the one quote in the initial post, but _id is null]