guard<R> method

R? guard<R>(
  1. R cb(
    1. T value
    )
)

Transform an object if that value is not null.

Doing:

Map? json;
var result = json?['key']?.guard((json) => Model.fromJson(json));

is equivalent to doing:

Map? json;
var key = json?['key'];
var result = key == null ? null : Model.fromJson(key);

Implementation

R? guard<R>(R Function(T value) cb) {
  if (this is T) return cb(this as T);
  return null;
}