ThrowingSupplierGet<T> extension

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');
}

This extension provides a .get() method to call the supplier in a more expressive and object-oriented way.

Instead of calling supplier(), you can write supplier.get(), improving readability in functional or builder-style code.

Example

final supplier = ThrowingSupplier<int>(() => 42);
print(supplier.get()); // prints 42
on

Methods

get() → T

Available on ThrowingSupplier<T>, provided by the ThrowingSupplierGet extension

Invokes the supplier and returns the result.