get<T> method
Retrieves the value of key
cast to type T
. Supports type-specific operations.
Supported types include List, bool, int, double, and String.
Implementation
T get<T>(String key, {T? def}) {
switch (T) {
case List:
var res = dataObject(key, def: def);
if (res is List) {
return res as T;
}
if (def != null) return def;
return [] as T;
case num:
var res = (num.tryParse(data(key)) ?? -1);
if (def != null && res == -1) return def;
return res as T;
case int:
var res = (int.tryParse(data(key)) ?? -1);
if (def != null && res == -1) return def;
return res as T;
case bool:
if (!hasData(key)) {
return (def != null ? def : false) as T;
}
return data(key).toBool as T;
case String:
var res = data(key).toString();
if (res.isEmpty && def != null) return def;
return res as T;
case double:
var res = data(key).toString().asDouble();
return res as T;
default:
return data(key) as T;
}
}