extract<T> method

Option<T> extract<T>(
  1. K key
)

Return an Option that conditionally accesses map keys, only if they match the given type. Useful for accessing nested JSON.

expect(
  { 'test': 123 }.extract<int>('test'),
  Option.of(123),
);
expect(
  { 'test': 'string' }.extract<int>('test'),
  Option.none(),
);

Implementation

Option<T> extract<T>(K key) {
  final value = this[key];
  if (value is T) return Option.of(value);
  return Option.none();
}