如何通过一对多连接创建 Flutter Moor 关系
How To Create Flutter Moor Relationship With One-To-Many Join
我有一个 table 类别,该类别与一对多关系中的任务 table 相关,我正在尝试使用 Moor 执行连接。
我想要 return 一个列表,用于匹配某个类别的任务列表。我该怎么做?
Stream<List<CategoryWithTask>> watchAllCategories() {
return (select(categories)
..orderBy(([
(c) => OrderingTerm(expression: c.name),
])))
.join([leftOuterJoin(tasks, tasks.categoryId.equalsExp(categories.id))])
.watch()
.map((rows) => rows.map(
(row) {
return CategoryWithTask(
category: row.readTable(categories),
task: row.readTable(tasks)); // How do I modify this line to return a list of tasks corresponding to a category?
},
).toList());
}
在 Github 上创建问题后,我在 Flutter Moor 的人的支持下设法找到了一种方法。下面是工作代码;
Stream<List<CategoryWithTasks>> watchAllCategories() {
return (select(categories)
..orderBy(([
(c) => OrderingTerm(expression: c.name),
])))
.join([leftOuterJoin(tasks, tasks.categoryId.equalsExp(categories.id))])
.watch()
.map((rows) {
final groupedData = <Category, List<Task>>{};
for (final row in rows) {
final category = row.readTable(categories);
final task = row.readTable(tasks);
final list = groupedData.putIfAbsent(category, () => []);
if (task != null) list.add(task);
}
return [
for (final entry in groupedData.entries)
CategoryWithTasks(category: entry.key, tasks: entry.value)
];
});
}
我有一个 table 类别,该类别与一对多关系中的任务 table 相关,我正在尝试使用 Moor 执行连接。
我想要 return 一个列表,用于匹配某个类别的任务列表。我该怎么做?
Stream<List<CategoryWithTask>> watchAllCategories() {
return (select(categories)
..orderBy(([
(c) => OrderingTerm(expression: c.name),
])))
.join([leftOuterJoin(tasks, tasks.categoryId.equalsExp(categories.id))])
.watch()
.map((rows) => rows.map(
(row) {
return CategoryWithTask(
category: row.readTable(categories),
task: row.readTable(tasks)); // How do I modify this line to return a list of tasks corresponding to a category?
},
).toList());
}
在 Github 上创建问题后,我在 Flutter Moor 的人的支持下设法找到了一种方法。下面是工作代码;
Stream<List<CategoryWithTasks>> watchAllCategories() {
return (select(categories)
..orderBy(([
(c) => OrderingTerm(expression: c.name),
])))
.join([leftOuterJoin(tasks, tasks.categoryId.equalsExp(categories.id))])
.watch()
.map((rows) {
final groupedData = <Category, List<Task>>{};
for (final row in rows) {
final category = row.readTable(categories);
final task = row.readTable(tasks);
final list = groupedData.putIfAbsent(category, () => []);
if (task != null) list.add(task);
}
return [
for (final entry in groupedData.entries)
CategoryWithTasks(category: entry.key, tasks: entry.value)
];
});
}