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:
- Implemented types
- Implementers
- Available extensions
- AsAsyncCodable
- AsAsyncDecodable
- AsFutureEncodable
- AsListCodable
- AsListDecodable
- AsListEncodable
- AsMapCodable
- AsNullableCodable
- AsNullableDecodable
- AsNullableListEncodable
- AsSetCodable
- AsSetDecodable
- AsSetEncodable
- CsvDecodable
- CsvEncodable
- JsonDecodable
- JsonEncodable
- MsgPackDecodable
- MsgPackEncodable
- ProgressiveJsonDecodable
- ProgressiveJsonEncodable
- StandardCodec
- StandardDecodable
- StandardEncodable
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 extensionno setter - hashCode → int
-
The hash code for this object.
no setterinherited
-
orNull
→ Codable<
T?> -
Available on Codable<
Returns a Codable that can encode and decodeT> , provided by the AsNullableCodable extensionT
or null.no setter -
orNull
→ Decodable<
T?> -
Available on Decodable<
Returns a Decodable object that can decodeT> , provided by the AsNullableDecodable extensionT
or null.no setter -
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
-
decode(
Decoder decoder) → T -
Decodes a value of type
T
using thedecoder
.inherited -
encode(
T value, Encoder encoder) → void -
Encodes the
value
using theencoder
.inherited -
fromCsv(
String csv) → List< T> -
Available on Decodable<
Decodes a CSV string into a list of objects.T> , provided by the CsvDecodable extension -
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<
Decodes progressive JSON bytes intoT> , provided by the ProgressiveJsonDecodable extensionT
. -
fromProgressiveJsonStream(
Stream< List< bytes) → Stream<int> >T> -
Available on Decodable<
Decodes a progressive JSON byte stream into a Stream ofT> , provided by the ProgressiveJsonDecodable extensionT
. -
fromValue(
Object? value) → T -
Available on Decodable<
T> , provided by the StandardDecodable extension -
future(
) → Decodable< Future< T> > -
Available on Decodable<
Returns a Decodable object that can decode a future ofT> , provided by the AsAsyncDecodable extensionT
. -
future(
) → Encodable< Future< T> > -
Available on Encodable<
Returns an Encodable that can encode a future ofT> , provided by the AsFutureEncodable extensionT
. -
future(
) → Codable< Future< T> > -
Available on Codable<
Returns a Codable that can encode and decode a future ofT> , provided by the AsAsyncCodable extensionT
. -
list(
) → Codable< List< T> > -
Available on Codable<
Returns a Codable that can encode and decode a list ofT> , provided by the AsListCodable extensionT
. -
list(
) → Decodable< List< T> > -
Available on Decodable<
Returns a Decodable object that can decode a list ofT> , provided by the AsListDecodable extensionT
. -
list(
) → Encodable< List< T> > -
Available on Encodable<
Returns an Encodable that can encode a list ofT> , provided by the AsListEncodable extensionT
. -
map<
K> ([Codable< K> ? keyCodable]) → Codable<Map< K, T> > -
Available on Codable<
Returns a Codable that can encode and decode a map ofT> , provided by the AsMapCodable extensionK
andT
. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
set(
) → Codable< Set< T> > -
Available on Codable<
Returns a Codable that can encode and decode a set ofT> , provided by the AsSetCodable extensionT
. -
set(
) → Decodable< Set< T> > -
Available on Decodable<
Returns a Decodable object that can decode a set ofT> , provided by the AsSetDecodable extensionT
. -
set(
) → Encodable< Set< T> > -
Available on Encodable<
Returns an Encodable that can encode a set ofT> , provided by the AsSetEncodable extensionT
. -
stream(
) → Decodable< Stream< T> > -
Available on Decodable<
Returns a Decodable object that can decode a stream ofT> , provided by the AsAsyncDecodable extensionT
. -
stream(
) → Encodable< Stream< T> > -
Available on Encodable<
Returns an Encodable that can encode a stream ofT> , provided by the AsFutureEncodable extensionT
. -
stream(
) → LazyCodable< Stream< T> > -
Available on Codable<
Returns a Codable that can encode and decode a stream ofT> , provided by the AsAsyncCodable 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