dataAs<X> method

X? dataAs<X>()

Get the data as an X, or null if it is something else.

An is check doesn't narrow a field like data, so use this to read one shape of a OneOf response.

Example:

final failed = response.dataAs<ValidationError>();
if (failed != null) form.setErrors(failed.errors);

Implementation

X? dataAs<X>() {
  final T? d = data;
  return d is X ? d : null;
}