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.
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<
Returns an Encodable that can encodeT> , provided by the AsNullableListEncodable extensionT
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 theencoder
. -
future(
) → Encodable< Future< T> > -
Available on Encodable<
Returns an Encodable that can encode a future ofT> , provided by the AsFutureEncodable extensionT
. -
list(
) → Encodable< List< T> > -
Available on Encodable<
Returns an Encodable that can encode a list ofT> , provided by the AsListEncodable extensionT
. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
set(
) → Encodable< Set< T> > -
Available on Encodable<
Returns an Encodable that can encode a set ofT> , provided by the AsSetEncodable extensionT
. -
stream(
) → Encodable< Stream< T> > -
Available on Encodable<
Returns an Encodable that can encode a stream ofT> , provided by the AsFutureEncodable extensionT
. -
toCsv(
Iterable< T> value) → String -
Available on Encodable<
Encodes a list of objects into a CSV string.T> , provided by the CsvEncodable extension -
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<
Encodes the value to a Stream of progressive JSON bytes.T> , provided by the ProgressiveJsonEncodable extension -
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