map<Dart> method Null safety

ColumnBuilder<T> map<Dart>(
  1. TypeConverter<Dart, T?> converter
)

Uses a custom converter to store custom Dart objects in a single column and automatically mapping them from and to sql.

An example might look like this:

 // this is the custom object with we want to store in a column. It
 // can be as complex as you want it to be
 class MyCustomObject {
  final String data;
  MyCustomObject(this.data);
}

class CustomConverter extends TypeConverter<MyCustomObject, String> {
  // this class is responsible for turning a custom object into a string.
  // this is easy here, but more complex objects could be serialized using
  // json or any other method of your choice.
  const CustomConverter();
  @override
  MyCustomObject fromSql(String fromDb) {
    return fromDb == null ? null : MyCustomObject(fromDb);
  }

  @override
  String toSql(MyCustomObject value) {
    return value?.data;
  }
}

In that case, you could have a table with this column

TextColumn get custom => text().map(const CustomConverter())();

The generated row class will then use a MyFancyClass instead of a String, which would usually be used for Table.text columns.

The type T of the type converter may only be nullable if this column was declared nullable too. Otherwise, drift_dev will emit an error.

Implementation

ColumnBuilder<T> map<Dart>(TypeConverter<Dart, T?> converter) =>
    _isGenerated();