Encodable<T> class abstract interface

An object that can encode a value of type T to various data formats.

The interface can be used like this:

class UriEncodable implements Encodable<Uri> {
  const UriEncodable();

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

The Encodable interface can be used for any type, especially ones that cannot be modified to implement SelfEncodable instead, like core or third-party types. It can also be used to separate the encoding logic from the type definition.

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

final Encodable<Uri> uriEncodable = const UriEncodable();
final Uri uri = ...;

// From 'package:codable/json.dart'
final String json = uriEncodable.toJson(uri);

// From 'package:codable/standard.dart'
final Object value = uriEncodable.toValue(uri);

// From imaginary 'package:x/x.dart'
final X x = uriEncodable.toX(uri);

See also:

  • SelfEncodable for self-encoding objects to various data formats.
  • Decodable for decoding values of some type from various data formats.
  • Codable for combining both encoding and decoding of types.
Implementers
Available extensions

Constructors

Encodable.fromHandler(void encode(T value, Encoder encoder))
Creates an Encodable from a handler function.
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
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

encode(T value, Encoder encoder) → void
Encodes the value using the encoder.
future() Encodable<Future<T>>

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

Returns an Encodable that can encode a future 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.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
set() Encodable<Set<T>>

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

Returns an Encodable that can encode a set 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.
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