asOrNull<R> method

R? asOrNull<R>()

Safely casts this value to type R.

Returns null if the cast is not possible instead of throwing an error.

Example:

final value = 'hello'.asOrNull<String>(); // 'hello'
final number = 'hello'.asOrNull<int>(); // null

Implementation

R? asOrNull<R>() => this is R ? this as R : null;