get<T> method

T get<T>()

Reads the ConstantReader instance and returns an instance of T.

Throws ErrorOf<ConstantReader> if an instance cannot be constructed.

Implementation

T get<T>() {
  if (T == dynamic) return getDynamic();

  if (isEnum<T>()) {
    final classMirror = reflectClass(T);
    final values = classMirror.getField(Symbol('values')).reflectee;
    final index = peek('index')?.intValue;

    if (index != null && dartType != null) {
      _resolvedTypes[dartType!] ??= T;
      return values[index];
    } else {
      throw ErrorOf<ConstantReader>(
          message: 'Could not read enum instance of type $T.');
    }
  }

  if (!holdsA<T>()) {
    throw ErrorOf<ConstantReader>(
        message: 'Input does not represent an object of type <$T>',
        invalidState: 'Input represents an object of '
            'type <$dartType>.');
  }
  if (!_decoders.containsKey(T)) {
    throw ErrorOf<ConstantReader>(
        message: 'Could not read value of type [$T].',
        invalidState: 'A decoder function for type [$T] is missing.',
        expectedState: 'Use addDecoder<$T>() to register a '
            'decoder function for type [$T].');
  }
  return _decoders[T]!(this);
}