Migrate remaining examples to null-safety

This commit is contained in:
Simon Binder 2022-07-01 21:51:33 +02:00
parent 52c07c9d3f
commit a064bb719d
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
5 changed files with 85 additions and 97 deletions

View File

@ -10,36 +10,33 @@ part of 'database.dart';
class User extends DataClass implements Insertable<User> {
final int id;
final String name;
User({@required this.id, @required this.name});
factory User.fromData(Map<String, dynamic> data, {String prefix}) {
User({required this.id, required this.name});
factory User.fromData(Map<String, dynamic> data, {String? prefix}) {
final effectivePrefix = prefix ?? '';
return User(
id: const IntType().mapFromDatabaseResponse(data['${effectivePrefix}id']),
id: const IntType()
.mapFromDatabaseResponse(data['${effectivePrefix}id'])!,
name: const StringType()
.mapFromDatabaseResponse(data['${effectivePrefix}name']),
.mapFromDatabaseResponse(data['${effectivePrefix}name'])!,
);
}
@override
Map<String, Expression> toColumns(bool nullToAbsent) {
final map = <String, Expression>{};
if (!nullToAbsent || id != null) {
map['id'] = Variable<int>(id);
}
if (!nullToAbsent || name != null) {
map['name'] = Variable<String>(name);
}
return map;
}
UsersCompanion toCompanion(bool nullToAbsent) {
return UsersCompanion(
id: id == null && nullToAbsent ? const Value.absent() : Value(id),
name: name == null && nullToAbsent ? const Value.absent() : Value(name),
id: Value(id),
name: Value(name),
);
}
factory User.fromJson(Map<String, dynamic> json,
{ValueSerializer serializer}) {
{ValueSerializer? serializer}) {
serializer ??= driftRuntimeOptions.defaultSerializer;
return User(
id: serializer.fromJson<int>(json['id']),
@ -47,7 +44,7 @@ class User extends DataClass implements Insertable<User> {
);
}
@override
Map<String, dynamic> toJson({ValueSerializer serializer}) {
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
serializer ??= driftRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'id': serializer.toJson<int>(id),
@ -55,7 +52,7 @@ class User extends DataClass implements Insertable<User> {
};
}
User copyWith({int id, String name}) => User(
User copyWith({int? id, String? name}) => User(
id: id ?? this.id,
name: name ?? this.name,
);
@ -85,11 +82,11 @@ class UsersCompanion extends UpdateCompanion<User> {
});
UsersCompanion.insert({
this.id = const Value.absent(),
@required String name,
required String name,
}) : name = Value(name);
static Insertable<User> custom({
Expression<int> id,
Expression<String> name,
Expression<int>? id,
Expression<String>? name,
}) {
return RawValuesInsertable({
if (id != null) 'id': id,
@ -97,7 +94,7 @@ class UsersCompanion extends UpdateCompanion<User> {
});
}
UsersCompanion copyWith({Value<int> id, Value<String> name}) {
UsersCompanion copyWith({Value<int>? id, Value<String>? name}) {
return UsersCompanion(
id: id ?? this.id,
name: name ?? this.name,
@ -129,19 +126,17 @@ class UsersCompanion extends UpdateCompanion<User> {
class Users extends Table with TableInfo<Users, User> {
@override
final GeneratedDatabase attachedDatabase;
final String _alias;
final String? _alias;
Users(this.attachedDatabase, [this._alias]);
final VerificationMeta _idMeta = const VerificationMeta('id');
GeneratedColumn<int> _id;
GeneratedColumn<int> get id =>
_id ??= GeneratedColumn<int>('id', aliasedName, false,
late final GeneratedColumn<int?> id = GeneratedColumn<int?>(
'id', aliasedName, false,
type: const IntType(),
requiredDuringInsert: false,
$customConstraints: 'NOT NULL PRIMARY KEY AUTOINCREMENT');
final VerificationMeta _nameMeta = const VerificationMeta('name');
GeneratedColumn<String> _name;
GeneratedColumn<String> get name =>
_name ??= GeneratedColumn<String>('name', aliasedName, false,
late final GeneratedColumn<String?> name = GeneratedColumn<String?>(
'name', aliasedName, false,
type: const StringType(),
requiredDuringInsert: true,
$customConstraints: 'NOT NULL');
@ -157,11 +152,11 @@ class Users extends Table with TableInfo<Users, User> {
final context = VerificationContext();
final data = instance.toColumns(true);
if (data.containsKey('id')) {
context.handle(_idMeta, id.isAcceptableOrUnknown(data['id'], _idMeta));
context.handle(_idMeta, id.isAcceptableOrUnknown(data['id']!, _idMeta));
}
if (data.containsKey('name')) {
context.handle(
_nameMeta, name.isAcceptableOrUnknown(data['name'], _nameMeta));
_nameMeta, name.isAcceptableOrUnknown(data['name']!, _nameMeta));
} else if (isInserting) {
context.missing(_nameMeta);
}
@ -171,7 +166,7 @@ class Users extends Table with TableInfo<Users, User> {
@override
Set<GeneratedColumn> get $primaryKey => {id};
@override
User map(Map<String, dynamic> data, {String tablePrefix}) {
User map(Map<String, dynamic> data, {String? tablePrefix}) {
return User.fromData(data,
prefix: tablePrefix != null ? '$tablePrefix.' : null);
}
@ -187,8 +182,7 @@ class Users extends Table with TableInfo<Users, User> {
abstract class _$Database extends GeneratedDatabase {
_$Database(QueryExecutor e) : super(SqlTypeSystem.defaultInstance, e);
Users _users;
Users get users => _users ??= Users(this);
late final Users users = Users(this);
@override
Iterable<TableInfo> get allTables => allSchemaEntities.whereType<TableInfo>();
@override

View File

@ -10,10 +10,10 @@ class _$Foo extends Foo {
@override
final User moorField;
factory _$Foo([void Function(FooBuilder) updates]) =>
factory _$Foo([void Function(FooBuilder)? updates]) =>
(new FooBuilder()..update(updates))._build();
_$Foo._({this.moorField}) : super._() {
_$Foo._({required this.moorField}) : super._() {
BuiltValueNullFieldError.checkNotNull(moorField, r'Foo', 'moorField');
}
@ -43,11 +43,11 @@ class _$Foo extends Foo {
}
class FooBuilder implements Builder<Foo, FooBuilder> {
_$Foo _$v;
_$Foo? _$v;
User _moorField;
User get moorField => _$this._moorField;
set moorField(User moorField) => _$this._moorField = moorField;
User? _moorField;
User? get moorField => _$this._moorField;
set moorField(User? moorField) => _$this._moorField = moorField;
FooBuilder();
@ -67,7 +67,7 @@ class FooBuilder implements Builder<Foo, FooBuilder> {
}
@override
void update(void Function(FooBuilder) updates) {
void update(void Function(FooBuilder)? updates) {
if (updates != null) updates(this);
}

View File

@ -3,14 +3,14 @@ publish_to: none
version: 1.0.0
environment:
sdk: '>=2.6.0 <3.0.0'
sdk: '>=2.12.0 <3.0.0'
dependencies:
drift: ^1.0.0
drift: ^2.0.0-dev
built_value: ^8.0.0
dev_dependencies:
drift_dev: ^1.0.0
drift_dev: ^2.0.0-dev
built_value_generator: ^8.1.1
build_runner: ^2.0.0

View File

@ -1,18 +1,18 @@
name: web
description: Run integration tests for Moor on the web
environment:
sdk: '>=2.6.0 <3.0.0'
sdk: '>=2.12.0 <3.0.0'
dependencies:
drift:
drift: ^2.0.0-dev
drift_testcases:
path: ../drift_testcases
dev_dependencies:
test: ^1.5.0
test: ^1.18.0
build_runner:
build_web_compilers:
drift_dev: ^1.0.0
drift_dev: ^2.0.0-dev
dependency_overrides:
drift:

View File

@ -9,44 +9,43 @@ part of 'saves_after_migration_regression_test.dart';
// ignore_for_file: type=lint
class Foo extends DataClass implements Insertable<Foo> {
final int id;
Foo({@required this.id});
factory Foo.fromData(Map<String, dynamic> data, {String prefix}) {
Foo({required this.id});
factory Foo.fromData(Map<String, dynamic> data, {String? prefix}) {
final effectivePrefix = prefix ?? '';
return Foo(
id: const IntType().mapFromDatabaseResponse(data['${effectivePrefix}id']),
id: const IntType()
.mapFromDatabaseResponse(data['${effectivePrefix}id'])!,
);
}
@override
Map<String, Expression> toColumns(bool nullToAbsent) {
final map = <String, Expression>{};
if (!nullToAbsent || id != null) {
map['id'] = Variable<int>(id);
}
return map;
}
FoosCompanion toCompanion(bool nullToAbsent) {
return FoosCompanion(
id: id == null && nullToAbsent ? const Value.absent() : Value(id),
id: Value(id),
);
}
factory Foo.fromJson(Map<String, dynamic> json,
{ValueSerializer serializer}) {
{ValueSerializer? serializer}) {
serializer ??= driftRuntimeOptions.defaultSerializer;
return Foo(
id: serializer.fromJson<int>(json['id']),
);
}
@override
Map<String, dynamic> toJson({ValueSerializer serializer}) {
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
serializer ??= driftRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'id': serializer.toJson<int>(id),
};
}
Foo copyWith({int id}) => Foo(
Foo copyWith({int? id}) => Foo(
id: id ?? this.id,
);
@override
@ -73,14 +72,14 @@ class FoosCompanion extends UpdateCompanion<Foo> {
this.id = const Value.absent(),
});
static Insertable<Foo> custom({
Expression<int> id,
Expression<int>? id,
}) {
return RawValuesInsertable({
if (id != null) 'id': id,
});
}
FoosCompanion copyWith({Value<int> id}) {
FoosCompanion copyWith({Value<int>? id}) {
return FoosCompanion(
id: id ?? this.id,
);
@ -107,13 +106,12 @@ class FoosCompanion extends UpdateCompanion<Foo> {
class $FoosTable extends Foos with TableInfo<$FoosTable, Foo> {
@override
final GeneratedDatabase attachedDatabase;
final String _alias;
final String? _alias;
$FoosTable(this.attachedDatabase, [this._alias]);
final VerificationMeta _idMeta = const VerificationMeta('id');
GeneratedColumn<int> _id;
@override
GeneratedColumn<int> get id =>
_id ??= GeneratedColumn<int>('id', aliasedName, false,
late final GeneratedColumn<int?> id = GeneratedColumn<int?>(
'id', aliasedName, false,
type: const IntType(),
requiredDuringInsert: false,
defaultConstraints: 'PRIMARY KEY AUTOINCREMENT');
@ -129,7 +127,7 @@ class $FoosTable extends Foos with TableInfo<$FoosTable, Foo> {
final context = VerificationContext();
final data = instance.toColumns(true);
if (data.containsKey('id')) {
context.handle(_idMeta, id.isAcceptableOrUnknown(data['id'], _idMeta));
context.handle(_idMeta, id.isAcceptableOrUnknown(data['id']!, _idMeta));
}
return context;
}
@ -137,7 +135,7 @@ class $FoosTable extends Foos with TableInfo<$FoosTable, Foo> {
@override
Set<GeneratedColumn> get $primaryKey => {id};
@override
Foo map(Map<String, dynamic> data, {String tablePrefix}) {
Foo map(Map<String, dynamic> data, {String? tablePrefix}) {
return Foo.fromData(data,
prefix: tablePrefix != null ? '$tablePrefix.' : null);
}
@ -150,44 +148,43 @@ class $FoosTable extends Foos with TableInfo<$FoosTable, Foo> {
class Bar extends DataClass implements Insertable<Bar> {
final int id;
Bar({@required this.id});
factory Bar.fromData(Map<String, dynamic> data, {String prefix}) {
Bar({required this.id});
factory Bar.fromData(Map<String, dynamic> data, {String? prefix}) {
final effectivePrefix = prefix ?? '';
return Bar(
id: const IntType().mapFromDatabaseResponse(data['${effectivePrefix}id']),
id: const IntType()
.mapFromDatabaseResponse(data['${effectivePrefix}id'])!,
);
}
@override
Map<String, Expression> toColumns(bool nullToAbsent) {
final map = <String, Expression>{};
if (!nullToAbsent || id != null) {
map['id'] = Variable<int>(id);
}
return map;
}
BarsCompanion toCompanion(bool nullToAbsent) {
return BarsCompanion(
id: id == null && nullToAbsent ? const Value.absent() : Value(id),
id: Value(id),
);
}
factory Bar.fromJson(Map<String, dynamic> json,
{ValueSerializer serializer}) {
{ValueSerializer? serializer}) {
serializer ??= driftRuntimeOptions.defaultSerializer;
return Bar(
id: serializer.fromJson<int>(json['id']),
);
}
@override
Map<String, dynamic> toJson({ValueSerializer serializer}) {
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
serializer ??= driftRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'id': serializer.toJson<int>(id),
};
}
Bar copyWith({int id}) => Bar(
Bar copyWith({int? id}) => Bar(
id: id ?? this.id,
);
@override
@ -214,14 +211,14 @@ class BarsCompanion extends UpdateCompanion<Bar> {
this.id = const Value.absent(),
});
static Insertable<Bar> custom({
Expression<int> id,
Expression<int>? id,
}) {
return RawValuesInsertable({
if (id != null) 'id': id,
});
}
BarsCompanion copyWith({Value<int> id}) {
BarsCompanion copyWith({Value<int>? id}) {
return BarsCompanion(
id: id ?? this.id,
);
@ -248,13 +245,12 @@ class BarsCompanion extends UpdateCompanion<Bar> {
class $BarsTable extends Bars with TableInfo<$BarsTable, Bar> {
@override
final GeneratedDatabase attachedDatabase;
final String _alias;
final String? _alias;
$BarsTable(this.attachedDatabase, [this._alias]);
final VerificationMeta _idMeta = const VerificationMeta('id');
GeneratedColumn<int> _id;
@override
GeneratedColumn<int> get id =>
_id ??= GeneratedColumn<int>('id', aliasedName, false,
late final GeneratedColumn<int?> id = GeneratedColumn<int?>(
'id', aliasedName, false,
type: const IntType(),
requiredDuringInsert: false,
defaultConstraints: 'PRIMARY KEY AUTOINCREMENT');
@ -270,7 +266,7 @@ class $BarsTable extends Bars with TableInfo<$BarsTable, Bar> {
final context = VerificationContext();
final data = instance.toColumns(true);
if (data.containsKey('id')) {
context.handle(_idMeta, id.isAcceptableOrUnknown(data['id'], _idMeta));
context.handle(_idMeta, id.isAcceptableOrUnknown(data['id']!, _idMeta));
}
return context;
}
@ -278,7 +274,7 @@ class $BarsTable extends Bars with TableInfo<$BarsTable, Bar> {
@override
Set<GeneratedColumn> get $primaryKey => {id};
@override
Bar map(Map<String, dynamic> data, {String tablePrefix}) {
Bar map(Map<String, dynamic> data, {String? tablePrefix}) {
return Bar.fromData(data,
prefix: tablePrefix != null ? '$tablePrefix.' : null);
}
@ -291,10 +287,8 @@ class $BarsTable extends Bars with TableInfo<$BarsTable, Bar> {
abstract class _$_FakeDb extends GeneratedDatabase {
_$_FakeDb(QueryExecutor e) : super(SqlTypeSystem.defaultInstance, e);
$FoosTable _foos;
$FoosTable get foos => _foos ??= $FoosTable(this);
$BarsTable _bars;
$BarsTable get bars => _bars ??= $BarsTable(this);
late final $FoosTable foos = $FoosTable(this);
late final $BarsTable bars = $BarsTable(this);
@override
Iterable<TableInfo> get allTables => allSchemaEntities.whereType<TableInfo>();
@override