TypeConverter<T, S> class Null safety

Base class for type converters which can be applied to:

  1. databases
  2. DAOs
  3. entities/views
  4. entity/view fields
  5. DAO methods
  6. DAO method parameters

The type converter is added to the scope of the element so if you put it on a class, all methods/fields in that class will be able to use the converter.

The closest type converter wins! If you, for example, add a converter on the database level and another one on a DAO method parameter, which takes care of converting the same types, the one declared next to the DAO method parameter will be used. Please refer to the above list to get more information about the precedence of converters.

A type converter that converts between DateTime and the database compatible int type can be seen in the following example.

// definition
class DateTimeConverter extends TypeConverter<DateTime, int> {
  @override
  DateTime decode(int databaseValue) {
    return DateTime.fromMillisecondsSinceEpoch(databaseValue);
  }

  @override
  int encode(DateTime value) {
    return value.millisecondsSinceEpoch;
  }
}

// usage
@TypeConverters([DateTimeConverter])
@Database(version: 1, entities: [Order])
abstract class OrderDatabase extends FloorDatabase {
  OrderDao get orderDao;
}

@entity
class Order {
  @primaryKey
  final int id;

  final DateTime date;

  Order(this.id, this.date);
}
Annotations

Constructors

TypeConverter()

Properties

hashCode int
The hash code for this object.
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

decode(S databaseValue) → T
Converts the databaseValue of type S into T
encode(T value) → S
Converts the value of type T into the database-compatible type S
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

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