asT<T> function
Implementation
T asT<T>(Map<String, dynamic>? json, String key, {T? defaultValue}) {
if (json == null || !json.containsKey(key)) {
if (defaultValue != null) return defaultValue;
if (0 is T) return 0 as T;
if (0.0 is T) return 0.0 as T;
if ('' is T) return '' as T;
if (false is T) return false as T;
if ([] is T) return [] as T;
if (<String, dynamic>{} is T) return <String, dynamic>{} as T;
return '' as T;
}
dynamic value = json[key];
if (value is T) return value;
if (0 is T) {
return toInt(value, defaultValue: (defaultValue ?? 0) as int) as T;
} else if (0.0 is T) {
return toDouble(value, defaultValue: (defaultValue ?? 0.0) as double) as T;
} else if ('' is T) {
return toString(value, defaultValue: (defaultValue ?? '') as String) as T;
} else if (false is T) {
return toBool(value, defaultValue: (defaultValue ?? false) as bool) as T;
} else if ([] is T) {
return toList(value, defaultValue: (defaultValue ?? []) as List) as T;
} else if (<String, dynamic>{} is T) {
Object defaultV = defaultValue ?? <String, dynamic>{};
return toMap(value, defaultValue: defaultV as Map<String, dynamic>) as T;
}
return '' as T;
}