safeCast<T extends Object> function

T? safeCast<T extends Object>(
  1. Object? value
)

Returns the value if it is the same type as T, otherwise null.

T must be non-nullable since the return type is nullable.

Implementation

T? safeCast<T extends Object>(Object? value) {
  return switch (value) {
    final T v => v,
    _ => null,
  };
}