参数类型'GeneratedIntColumn'无法赋值给参数类型'int':Moor-Flutter
The argument type 'GeneratedIntColumn' can't be assigned to the parameter type 'int': Moor-Flutter
我试图在连接两个表时设置与 Moor-Flutter 的外键关系,但在 VS Code 中出现以下错误;
The argument type 'GeneratedIntColumn' can't be assigned to the parameter type 'int'.
起初我以为是因为我使用自增字段作为外键,但现在我不太确定了,因为我更改代码时仍然出现错误。
这是我的代码。比较时,错误发生在连接语句上; tasks.id.equals(categories.taskId).
import 'package:moor/moor.dart';
import 'package:moor_flutter/moor_flutter.dart';
part 'moor_database.g.dart';
@DataClassName("Category")
class Categories extends Table {
IntColumn get id => integer().autoIncrement()();
IntColumn get taskId =>
integer().nullable().customConstraint('NULL REFERENCES tasks(id)')();
TextColumn get name => text().withLength(min: 1, max: 100)();
TextColumn get icon => text()();
TextColumn get color => text()();
}
class Tasks extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get name => text().withLength(min: 1, max: 200)();
BoolColumn get completed => boolean().withDefault(Constant(false))();
}
@UseMoor(tables: [Categories, Tasks], daos: [CategoryDao])
class AppDatabase extends _$AppDatabase {
AppDatabase()
: super((FlutterQueryExecutor.inDatabaseFolder(
path: 'todo.sqlite',
logStatements: true,
)));
@override
int get schemaVersion => 1;
@override
MigrationStrategy get migration =>
MigrationStrategy(beforeOpen: (details) async {
await customStatement('PRAGMA foreign_keys = ON');
});
}
class CategoryWithTask {
final Category category;
final Task task;
CategoryWithTask({@required this.category, @required this.task});
}
@UseDao(tables: [Categories, Tasks])
class CategoryDao extends DatabaseAccessor<AppDatabase>
with _$CategoryDaoMixin {
final AppDatabase db;
CategoryDao(this.db) : super(db);
Stream<List<CategoryWithTask>> watchAllCategories() {
return (select(categories)
..orderBy(([
(c) => OrderingTerm(expression: c.name),
])))
.join([leftOuterJoin(tasks, tasks.id.equals(categories.taskId))])
.watch()
.map((rows) => rows.map((row) {
return CategoryWithTask(
category: row.readTable(categories),
task: row.readTable(tasks));
}));
}
}
中的代码示例,我意识到了我的错误
而不是tasks.id.equals(categories.taskId)
联接中的表达式应为tasks.id.equalsExp(categories.taskId)
我试图在连接两个表时设置与 Moor-Flutter 的外键关系,但在 VS Code 中出现以下错误;
The argument type 'GeneratedIntColumn' can't be assigned to the parameter type 'int'.
起初我以为是因为我使用自增字段作为外键,但现在我不太确定了,因为我更改代码时仍然出现错误。
这是我的代码。比较时,错误发生在连接语句上; tasks.id.equals(categories.taskId).
import 'package:moor/moor.dart';
import 'package:moor_flutter/moor_flutter.dart';
part 'moor_database.g.dart';
@DataClassName("Category")
class Categories extends Table {
IntColumn get id => integer().autoIncrement()();
IntColumn get taskId =>
integer().nullable().customConstraint('NULL REFERENCES tasks(id)')();
TextColumn get name => text().withLength(min: 1, max: 100)();
TextColumn get icon => text()();
TextColumn get color => text()();
}
class Tasks extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get name => text().withLength(min: 1, max: 200)();
BoolColumn get completed => boolean().withDefault(Constant(false))();
}
@UseMoor(tables: [Categories, Tasks], daos: [CategoryDao])
class AppDatabase extends _$AppDatabase {
AppDatabase()
: super((FlutterQueryExecutor.inDatabaseFolder(
path: 'todo.sqlite',
logStatements: true,
)));
@override
int get schemaVersion => 1;
@override
MigrationStrategy get migration =>
MigrationStrategy(beforeOpen: (details) async {
await customStatement('PRAGMA foreign_keys = ON');
});
}
class CategoryWithTask {
final Category category;
final Task task;
CategoryWithTask({@required this.category, @required this.task});
}
@UseDao(tables: [Categories, Tasks])
class CategoryDao extends DatabaseAccessor<AppDatabase>
with _$CategoryDaoMixin {
final AppDatabase db;
CategoryDao(this.db) : super(db);
Stream<List<CategoryWithTask>> watchAllCategories() {
return (select(categories)
..orderBy(([
(c) => OrderingTerm(expression: c.name),
])))
.join([leftOuterJoin(tasks, tasks.id.equals(categories.taskId))])
.watch()
.map((rows) => rows.map((row) {
return CategoryWithTask(
category: row.readTable(categories),
task: row.readTable(tasks));
}));
}
}
而不是tasks.id.equals(categories.taskId)
联接中的表达式应为tasks.id.equalsExp(categories.taskId)