mapFromCompanion method

Row mapFromCompanion(
  1. Insertable<Row> companion,
  2. DriftDialect dialect
)

Converts a companion to the real model class, Row.

Values that are Value.absent in the companion will be set to null. The dialect is used so that the raw values from the companion can properly be interpreted as the high-level Dart values exposed by the data class.

Implementation

Row mapFromCompanion(Insertable<Row> companion, DriftDialect dialect) {
  final asColumnMap = companion.toColumns(false);

  if (asColumnMap.values.any((e) => e is! Variable)) {
    throw ArgumentError(
      'The companion $companion cannot be transformed '
      'into a dataclass as it contains expressions that need to be '
      'evaluated by a database engine.',
    );
  }

  final structure = ResultSetStructure()..addResultSet(this);
  final values = [
    for (final value in asColumnMap.values)
      (value as Variable).resolveValue(dialect),
  ];

  final mapper = createMapperToDart(dialect, structure);
  return mapper(values)!;
}