Moor: Generator does not respect nullability for type converters

Created on 23 Oct 2020  路  2Comments  路  Source: simolus3/moor

i have users table

class Users extends BaseEntity with RemoteIdEntity {
  BoolColumn get isMe =>
      boolean().named("is_me").withDefault(const Constant(false))();

  TextColumn get displayName => text().nullable().named("display_name")();

  TextColumn get fullName => text().nullable().named("full_name")();

  IntColumn get gender => intEnum<GenderType?>().nullable().named("gender")();

  DateTimeColumn get birthdate => dateTime().nullable().named("birthdate")();

  TextColumn get phoneNumber => text()
      .nullable()
      .map<Contact?>(ContactConverter())
      .named('phone_number')();
}

and the converter

class ContactConverter extends TypeConverter<Contact?, String?> {
  ContactConverter();

  @override
  Contact? mapToDart(String? fromDb) => fromDb != null
      ? Contact.fromJson(json.decode(fromDb) as Map<String, dynamic>)
      : null;

  @override
  String? mapToSql(Contact? value) =>
      value == null ? null : json.encode(value.toJson());
}

but the generated code had that error

static TypeConverter<Contact?, String> $converter1 = ContactConverter();
//should generate static TypeConverter<Contact?, String?> $converter1 = ContactConverter(); to work

what's happening here?

bug generator

All 2 comments

There is no support for NNBD yet. It will take some time to implement this once all the technical previews are through and NNBD at least in a beta. See https://github.com/simolus3/moor/issues/844

While the generator can generate NNBD code in _some_ scenarios, that's still very experimental. Since the core moor package hasn't migrated yet, I wouldn't recommend to use that. If you want to preview NNBD for most of your app, you can add //@dart=2.9 at the top of Dart files in which you define database classes or daos to exclude them from the experiment.

This is a valid bug report (thanks for reporting it!), but I want to point out that NNBD support in the generator isn't complete yet, so some problems are expected.

Was this page helpful?
0 / 5 - 0 ratings