tryDecodeAd function
Tries to decode ad data from the provided JSON data
.
If data
is not null, attempts to decode it as JSON.
If decoding is successful and the resulting JSON object is a valid map,
returns a FastResponseAd instance created from the JSON data.
If any error occurs during decoding or conversion, prints the error
and returns null.
Implementation
FastResponseAd? tryDecodeAd(String? data) {
if (data != null) {
try {
final jsonData = json.decode(data);
// Check if the response contains valid data as a Map.
if (jsonData is Map<String, dynamic>) {
return FastResponseAd.fromJson(jsonData);
}
} catch (error) {
debugPrint(error.toString());
}
}
return null;
}