Codable<T> class abstract

An object that can both encode and decode a value of type T to/from various data formats.

It combines both Encodable and Decodable into a single interface and can be used like this:

class PersonCodable implements Codable<Person> {
  const PersonCodable();

  @override
  void encode(Person value, Encoder encoder) {
    /* ... */
  }

  @override
  Person decode(Decoder decoder) {
    /* ... */
  }
}

Objects implementing Codable can encode and decode values to various data formats using the respective to<Format>(T value) and T from<Format>(data) extension methods. A particular method is available by importing the appropriate library or package:

final Codable<Person> codable = const PersonCodable();

// From 'package:codable/json.dart'
final Person person = codable.fromJson('...');
final String json = codable.toJson(person);

// From 'package:codable/standard.dart'
final Person person = codable.fromMap({...});
final Map<String, dynamic> map = codable.toMap(person);

// From imaginary 'package:x/x.dart'
final Person person = codable.fromX(x);
final X x = codable.toX(person);

See also:

  • Encodable for encoding values of some type to various data formats.
  • Decodable for decoding values of some type from various data formats.
Implemented types
Implementers
Available extensions

Constructors

Codable.new()
const
Codable.fromHandlers({required T decode(Decoder decoder), required void encode(T value, Encoder encoder)})
Creates a Codable from a pair of handler functions.
factory

Properties

codec Codec<T, Object?>

Available on Codable<T>, provided by the StandardCodec extension

no setter
hashCode int
The hash code for this object.
no setterinherited
orNull Codable<T?>

Available on Codable<T>, provided by the AsNullableCodable extension

Returns a Codable that can encode and decode T or null.
no setter
orNull Decodable<T?>

Available on Decodable<T>, provided by the AsNullableDecodable extension

Returns a Decodable object that can decode T or null.
no setter
orNull Encodable<T?>

Available on Encodable<T>, provided by the AsNullableListEncodable extension

Returns an Encodable that can encode T or null.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

decode(Decoder decoder) → T
Decodes a value of type T using the decoder.
inherited
encode(T value, Encoder encoder) → void
Encodes the value using the encoder.
inherited
fromCsv(String csv) List<T>

Available on Decodable<T>, provided by the CsvDecodable extension

Decodes a CSV string into a list of objects.
fromCsvBytes(List<int> bytes) List<T>

Available on Decodable<T>, provided by the CsvDecodable extension

fromJson(String json) → T

Available on Decodable<T>, provided by the JsonDecodable extension

fromJsonBytes(List<int> bytes) → T

Available on Decodable<T>, provided by the JsonDecodable extension

fromMap(Map<String, dynamic> map) → T

Available on Decodable<T>, provided by the StandardDecodable extension

fromMsgPack(List<int> bytes) → T

Available on Decodable<T>, provided by the MsgPackDecodable extension

fromProgressiveJson(List<int> bytes) → T

Available on Decodable<T>, provided by the ProgressiveJsonDecodable extension

Decodes progressive JSON bytes into T.
fromProgressiveJsonStream(Stream<List<int>> bytes) Stream<T>

Available on Decodable<T>, provided by the ProgressiveJsonDecodable extension

Decodes a progressive JSON byte stream into a Stream of T.
fromValue(Object? value) → T

Available on Decodable<T>, provided by the StandardDecodable extension

future() Decodable<Future<T>>

Available on Decodable<T>, provided by the AsAsyncDecodable extension

Returns a Decodable object that can decode a future of T.
future() Encodable<Future<T>>

Available on Encodable<T>, provided by the AsFutureEncodable extension

Returns an Encodable that can encode a future of T.
future() Codable<Future<T>>

Available on Codable<T>, provided by the AsAsyncCodable extension

Returns a Codable that can encode and decode a future of T.
list() Codable<List<T>>

Available on Codable<T>, provided by the AsListCodable extension

Returns a Codable that can encode and decode a list of T.
list() Decodable<List<T>>

Available on Decodable<T>, provided by the AsListDecodable extension

Returns a Decodable object that can decode a list of T.
list() Encodable<List<T>>

Available on Encodable<T>, provided by the AsListEncodable extension

Returns an Encodable that can encode a list of T.
map<K>([Codable<K>? keyCodable]) Codable<Map<K, T>>

Available on Codable<T>, provided by the AsMapCodable extension

Returns a Codable that can encode and decode a map of K and T.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
set() Codable<Set<T>>

Available on Codable<T>, provided by the AsSetCodable extension

Returns a Codable that can encode and decode a set of T.
set() Decodable<Set<T>>

Available on Decodable<T>, provided by the AsSetDecodable extension

Returns a Decodable object that can decode a set of T.
set() Encodable<Set<T>>

Available on Encodable<T>, provided by the AsSetEncodable extension

Returns an Encodable that can encode a set of T.
stream() Decodable<Stream<T>>

Available on Decodable<T>, provided by the AsAsyncDecodable extension

Returns a Decodable object that can decode a stream of T.
stream() Encodable<Stream<T>>

Available on Encodable<T>, provided by the AsFutureEncodable extension

Returns an Encodable that can encode a stream of T.
stream() LazyCodable<Stream<T>>

Available on Codable<T>, provided by the AsAsyncCodable extension

Returns a Codable that can encode and decode a stream of T.
toCsv(Iterable<T> value) String

Available on Encodable<T>, provided by the CsvEncodable extension

Encodes a list of objects into a CSV string.
toCsvBytes(Iterable<T> value) List<int>

Available on Encodable<T>, provided by the CsvEncodable extension

toJson(T value) String

Available on Encodable<T>, provided by the JsonEncodable extension

toJsonBytes(T value) List<int>

Available on Encodable<T>, provided by the JsonEncodable extension

toMap(T value) Map<String, dynamic>

Available on Encodable<T>, provided by the StandardEncodable extension

toMsgPack(T value) List<int>

Available on Encodable<T>, provided by the MsgPackEncodable extension

toProgressiveJson(T value) List<int>

Available on Encodable<T>, provided by the ProgressiveJsonEncodable extension

toProgressiveJsonStream(T value) Stream<List<int>>

Available on Encodable<T>, provided by the ProgressiveJsonEncodable extension

Encodes the value to a Stream of progressive JSON bytes.
toString() String
A string representation of this object.
inherited
toValue(T value) Object?

Available on Encodable<T>, provided by the StandardEncodable extension

Operators

operator ==(Object other) bool
The equality operator.
inherited