Skip to content

Instantly share code, notes, and snippets.

@sunderee
sunderee / try_catch.dart
Created February 24, 2025 12:43
Native Dart helpers that use records to return the result or thrown error/exception object.
(T?, Object?) tryCatch<T extends Object>(T Function() function) {
try {
final result = function.call();
return (result, null);
} catch (error) {
return (null, error);
}
}
Future<(T?, Object?)> tryCatchAsync<T extends Object>(