I have tried to decode a json file which is coming back from server after my request, but when i want to decode the json it show an error here is my decode codes:
var loginJson = jsonDecode(utf8.decode(response.bodyBytes));
var model = idResponseModel(
loginJson['id'],
loginJson["fname"],
loginJson["lname"],
loginJson["nationalCode"],
loginJson["gender"],
loginJson["bornDate"],
loginJson["phoneNumber"],
loginJson["cardNumber"],
loginJson["enabled"]);
print(model.id);
and console says it happened in loginJson['id']
the id from server is some thing like this :
"id": "5de0a41e-9a6f-4b55-9567-024cd0fdbfc5"
CodePudding user response:
slightly surprised by the direct use of [bodyBytes] try using the Response getter → body
String get body => _encodingForHeaders(headers).decode(bodyBytes);
in your case it should look like this:
var loginJson = jsonDecode(response.body);
var model = idResponseModel(
loginJson['id'],
loginJson["fname"],
loginJson["lname"],
loginJson["nationalCode"],
loginJson["gender"],
loginJson["bornDate"],
loginJson["phoneNumber"],
loginJson["cardNumber"],
loginJson["enabled"]);
print(model.id);
I have no way to check it now but if it works let me know
CodePudding user response:
This error means that loginJson.[]
expects an int, not a String. So it's possible that loginJson
is actually a List, and not a Map like you expect it to be. You can check by printing loginJson.runtimeType
.
CodePudding user response:
Unless you'd post the whole JSON, this question is insufficient and not reproducible... because you might believe you have the response already, while trying to get data from the wrong one node (id
is a string index, but it encounters an unexpected numeric index). That's exactly why your sample data and the error message do not provide the least meaning in combination.