as<T> abstract method

T as<T>({
  1. String? format,
})

Returns the content of this snapshot as an object of type T.

When the content is null or of type T, the content will be returned as is. Otherwise, a factory function registered in the SnapshotDecoder class will be used to convert the raw content to an object of type T. When no suitable factory function is found or the conversion fails, an error is thrown.

When format is specified, only factory functions that can handle this format will be used. For example,

snapshot.as<DateTime>(format: 'epoch') // will interpret content as millis since epoch
snapshot.as<DateTime>(format: 'dd/MM/yyyy') // will convert string content to according to specified date format
snapshot.as<double>(format: 'string') // will parse string content as double

The result of the conversion is cached, so that subsequent calls to as with the same type parameter and format, returns the exact same object.

Implementation

T as<T>({String? format});