safeFuture<T> static method
Safely executes a Future operation with automatic error handling
Stream closure errors are silently handled to prevent app crashes. Other errors are recorded and rethrown.
Implementation
static Future<T> safeFuture<T>(
Future<T> Function() operation, {
T? fallbackValue,
bool rethrowOnError = true,
}) async {
try {
return await operation();
} catch (error, stackTrace) {
if (isStreamClosureError(error)) {
// Stream was cancelled/closed, don't crash the app
if (fallbackValue != null) {
return fallbackValue;
}
rethrow;
}
// Record other errors for debugging
NewrelicMobile.instance.recordError(error, stackTrace);
if (rethrowOnError) {
rethrow;
}
if (fallbackValue != null) {
return fallbackValue;
}
rethrow;
}
}