ThrowingSupplier<T> typedef
ThrowingSupplier<T> =
T Function()
A functional interface that supplies a value and may throw an exception.
This typedef represents a function that takes no parameters and returns
a value of type T, possibly throwing an exception.
This is especially useful in APIs where you want to defer the execution of a potentially risky or expensive operation, such as file access, database queries, or complex computations.
Example
final supplier = ThrowingSupplier<String>(() {
if (DateTime.now().second % 2 == 0) {
throw Exception('Random failure');
}
return 'Success!';
});
try {
final result = supplier.get();
print('Result: $result');
} catch (e) {
print('Error occurred: $e');
}
Implementation
typedef ThrowingSupplier<T> = T Function();