extractErrorCode function
Extracts an error code from a JSON response body.
This function takes a JSON response body as input and attempts to extract
an error code. If the input body is not null and contains a key named 'code',
the value associated with the 'code' key is extracted as a string. The extracted
string is then parsed to an integer using int.tryParse
. If parsing is successful,
the integer value is returned. If parsing fails or the 'code' key is not present,
a default error code of 11 is returned.
@param body The JSON response body from which to extract the error code. @return An integer error code extracted from the JSON response body, or a default value of 11.
Implementation
int extractErrorCode(Map<String, dynamic>? body) {
if (body != null && body.containsKey('code')) {
final String code = body['code']?.toString() ?? '';
return int.tryParse(code) ?? 11;
} else {
return 11;
}
}