Skip to content

Instantly share code, notes, and snippets.

@alexmercerind
Last active April 1, 2025 15:19
Show Gist options
  • Save alexmercerind/56b0be81e6cbc78646b2e3b2d5d10ab4 to your computer and use it in GitHub Desktop.
Save alexmercerind/56b0be81e6cbc78646b2e3b2d5d10ab4 to your computer and use it in GitHub Desktop.
Future<O> _read<I, O>(String key, Map<String, dynamic> defaults, [O Function(I)? map]) async {
if (I == O) {
map ??= (value) => value as O;
} else if (map == null) {
throw ArgumentError();
}
try {
return switch (I) {
const (bool) => map(await db.getBoolean(key) as I),
const (int) => map(await db.getInteger(key) as I),
const (double) => map(await db.getDouble(key) as I),
const (String) => map(await db.getString(key) as I),
_ => map(await db.getJson(key) as I),
};
} catch (exception, stacktrace) {
debugPrint(exception.toString());
debugPrint(stacktrace.toString());
return defaults[key];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment