getValue<T> static method
Reads a value back out of shared storage.
The store is not write-only: the widget writes to it too. An
MToggleAction flips its bool on-device with the app closed, and a
declared refresh: source stores what it fetched — neither is visible to
the app without reading it back.
T may be String, bool, int, double, or List<dynamic> /
Map<String, dynamic> for values written with saveList / saveJson.
Returns defaultValue when the key is absent or holds another type.
Implementation
static Future<T?> getValue<T>(String key, {T? defaultValue}) async {
try {
final raw = await _channel.invokeMethod<Object?>('getValue', {
'key': key,
'appGroupId': _appGroupId,
});
if (raw == null) return defaultValue;
if (raw is T) return raw as T;
// Android keeps everything as strings, so coerce rather than fail.
final text = raw.toString();
if (T == String) return text as T;
if (T == bool) {
return (text == 'true' || text == '1') as T;
}
if (T == int) return (int.tryParse(text) ?? defaultValue) as T?;
if (T == double) return (double.tryParse(text) ?? defaultValue) as T?;
// Parenthesised: bare `T == List<dynamic>` parses the angle bracket as a
// comparison operator.
if (T == (List<dynamic>) || T == (Map<String, dynamic>)) {
return jsonDecode(text) as T;
}
return defaultValue;
} on PlatformException {
return defaultValue;
} on FormatException {
return defaultValue;
}
}