0

我刚刚开始使用Moor Database for Flutter。我将加入我的两个表以从两个表中获取一些列。

我检查了文档中给出的示例如下:

// we define a data class to contain both a todo entry and the associated category
class EntryWithCategory {
  EntryWithCategory(this.entry, this.category);

  final TodoEntry entry;
  final Category category;
}

// in the database class, we can then load the category for each entry
Stream<List<EntryWithCategory>> entriesWithCategory() {
  final query = select(todos).join([
    leftOuterJoin(categories, categories.id.equalsExp(todos.category)),
  ]);

  // see next section on how to parse the result
}

我不明白该把这门课放在哪里。如果我正在创建一个新类,那么它会给我一个错误,即找不到select关键字。还尝试导入与 moor 相关但不起作用。

我可以在哪里编写连接查询并制作此类?

4

1 回答 1

0

Moor basically says to get the results and build the class manually. That class has no relationship with the database, so you can put it wherever you want. It is just the suggested way of doing it.

So, the select statement returns an object that you can iterate over the resulting rows, as an SQL response. And with that results build the classes that will be returned.

Look at the next example of the docs:

return query.watch().map((rows) {
  return rows.map((row) {
    return EntryWithCategory(
      row.readTable(todos),
      row.readTableOrNull(categories),
    );
  }).toList();
});

Because is a stream, calls watch(),and then map().

This first map returns the result of the query, properly name rows, every time one of the rows changes in the database, it will emit all the rows again.

The second map inside the first is for turning every row into a EntryWithCategory object. That way the whole function returns a list of those object updated with every change.

于 2021-08-01T02:58:29.823 回答