Below is my code where I need to access the 'code' json object from response body and show the alert but my code is failing and throwing Invalid response(executing only the else part). The execution is failing to enter the if part.How do I fix it? Json response is as follows:{ "synchData": { "message": "Retailer already synced", "code": 1 } }
Future<Either<Failure, Q>> parseResponse<Q, R>(
http.Response response, ComputeCallback<String, R> callback) async {
if (response == null) {
print('response is null ');
return Left(UnknownError());
} else {
log('callback : ${callback.toString()}response.statusCode : ${response.statusCode} | response.body ${response.body}');
try {
switch (response.statusCode) {
case 200:
{
final Map<String, dynamic> body = json.decode(response.body);
print("body= $body");
if (body.containsKey("code")) {
BaseResponse baseResponse = BaseResponse.fromJson(body);
print("baseResponse= $baseResponse");
if (baseResponse.code != 0) {
if (baseResponse.refreshTokenExpired) {
return Left(SessionExpiry(message: baseResponse.message));
} else if (baseResponse.userResigned) {
return Left(UserResigned(message: baseResponse.message));
}
return Left(ServerValidation(message: baseResponse.message));
} else {
var result = await compute(callback, response.body);
return Right(result as Q);
}
} else {
return Left(InvalidResponse());
}
}
break;
case 401:
return Left(ForbiddenError());
break;
case 403:
return Left(UnAuthorizedError());
break;
case 404:
return Left(ServerError(
statusCode: response.statusCode, message: "File not found"));
break;
case 500:
return Left(ServerError(
statusCode: response.statusCode, message: "Server Failure"));
break;
default:
return Left(UnknownError(
statusCode: response.statusCode, message: response.body));
}
} catch (e) {
// if (kDebugMode) {
// throw e;
// }
return Left(UnknownError());
}
}
else if ( response.body["code"] ) ==1 { show alert }response.bodyhas only one entry with key "synchData". That's whybody.containsKey("code")is alwaysfalse.