【问题标题】:How to get the number of entries in a table using flutter_moor?如何使用flutter_moor获取表中的条目数?
【发布时间】:2021-06-14 14:59:18
【问题描述】:

当我向表格添加条目时,我需要知道表格中的元素数量。

onPressed: () {
  final db = Provider.of<AppDb>(context);

  final habitCount = 0; /* Number of entries in the "Habits" table */
  db.into(db.habits)
    .insert(HabitsCompanion.insert(name: "Sleep", order: habitCount * 2 ));
},

我怎样才能尽可能轻松地做到这一点?

【问题讨论】:

标签: flutter dart flutter-moor


【解决方案1】:

您可以创建 DAO 来管理所有查询。这里我给你一个文档中使用的 todos 表的例子。

在本例中,您可以通过 queries 键创建手动查询。

@UseDao(tables: [Todos], queries: {'totalTodos': 'SELECT COUNT(*) FROM todos;'})
class TodoDao extends DatabaseAccessor<MyDatabase> with _$TodoDaoMixin  {

  final MyDatabase database;

  TodoDao(this.database) : super(database);

  Future<int> getTotalRecords() {
    return totalTodos().getSingle();
  }
}

使用方法:

Selectable<int> total = TodoDao(database).totalTodos();
total.getSingle().then((value) => print('Records: $value'));

希望你能理解这个例子。如果您有任何问题,请告诉我。

【讨论】:

    猜你喜欢
    • 2011-02-11
    • 1970-01-01
    • 2019-10-20
    • 2011-11-09
    • 1970-01-01
    • 2021-09-15
    • 2020-08-04
    • 2016-06-03
    相关资源
    最近更新 更多