import 'dart:async';
import 'package:moor_flutter/moor_flutter.dart';
part 'tables.g.dart';
//Adds a Categorys table...
class Categorys extends Table {
IntColumn get cid => integer().named('cid').autoIncrement()();
TextColumn get ctitle => text().named('ctitle')();
DateTimeColumn get ctimestamp => dateTime().named('ctimestamp')();
TextColumn get mategorymtitle =>
text().nullable().customConstraint('NULL REFERENCES mategorys(mtitle)')();
}
//Adds a Mategory table...
class Mategorys extends Table {
//IntColumn get mid => integer().named('mid').autoIncrement()();
TextColumn get mtitle => text().named('mtitle')();
DateTimeColumn get mtimestamp => dateTime().named('mtimestamp')();
//IntColumn get cid => integer().named('cid')();
@override
Set<Column> get primaryKey => {mtitle};
}
//Adds a CategorywithMategory class...
class CategoryWithMategory {
final Category category;
final Mategory mategory;
CategoryWithMategory({
@required this.category,
@required this.mategory,
});
}
//Adds a MyDatabase class...
@UseMoor(tables: [Categorys, Mategorys], daos: [CategorysDao, MategorysDao])
class MyDatabase extends _$MyDatabase {
MyDatabase()
: super(FlutterQueryExecutor.inDatabaseFolder(
path: 'db.sqlite',
));
@override
int get schemaVersion => 1;
}
//Adds a CategorysDao name...
@UseDao(tables: [Categorys,Mategorys])
class CategorysDao extends DatabaseAccessor<MyDatabase>
with _$CategorysDaoMixin {
CategorysDao(MyDatabase db) : super(db);
Stream<List<CategoryWithMategory>> watchAllCategorys() {
return (select(categorys)
..orderBy(
[
(t) => OrderingTerm(expression: t.cid, mode: OrderingMode.asc),
(t) => OrderingTerm(expression: t.mategorymtitle),
],
))
.join(
[
leftOuterJoin(mategorys,
mategorys.mtitle.equalsExp(categorys.mategorymtitle)),
],
)
.watch()
.map((rows) => rows.map(
(row) {
return CategoryWithMategory(
category: row.readTable(categorys),
mategory: row.readTable(mategorys),
);
},
).toList());
}
Future<List<Category>> get allWatchingCategorys => select(categorys).get();
//Adds a category name...
void addCategory({String ctitle, DateTime ctimestamp}) {
final _entry = CategorysCompanion(
ctitle: Value(ctitle),
ctimestamp: Value(ctimestamp),
);
into(categorys).insert(_entry);
}
//Updates a category name...
void updateCategory({String ctitle, DateTime ctimestamp}) {
final _entry = CategorysCompanion(
ctitle: Value(ctitle),
ctimestamp: Value(ctimestamp),
);
update(categorys).write(_entry);
}
}
//Adds a MategoryDao name...
class MategorysDao extends DatabaseAccessor<MyDatabase>
with _$MategorysDaoMixin {
MategorysDao(MyDatabase db) : super(db);
Future<List<Mategory>> get allWatchingMategorys => select(mategorys).get();
Stream<List<Mategory>> get watchAllMategorys => select(mategorys).watch();
//Adds a mategory name...
void addMategory({String mtitle, DateTime mtimestamp}) {
final _entry = MategorysCompanion(
mtitle: Value(mtitle),
mtimestamp: Value(mtimestamp),
);
into(mategorys).insert(_entry);
}
//Updates a mategory name...
void updateMategory({String mtitle, DateTime mtimestamp}) {
final _entry = MategorysCompanion(
mtitle: Value(mtitle),
mtimestamp: Value(mtimestamp),
);
update(mategorys).write(_entry);
}
}
**我在 android studio 的 dart 中制作了这个程序。它有两个表 Categorys 和 Mategorys。Mategorys 只不过是类别的子列表。Flutter 中的上述程序在 CatgorysDao 类中显示错误,在 watchAllCategorys 方法中的 leftouterjoin 中,由于 mategorys。无法理解为什么。请问有人能找出错误吗?我无法使用颤振中的 moor 库编写具有两个或多个表的正确程序。请帮忙...