countElementsOf<T> method

Future<int> countElementsOf<T>({
  1. String expression = '*',
  2. String? afterWhere,
})

Counts rows of T, optionally filtered by afterWhere.

  • expression: expression passed to COUNT(...); defaults to '*'. Use 'DISTINCT column' to count distinct values of a column.

Throws DatabaseException if the entity table does not exist.

Implementation

Future<int> countElementsOf<T>({
  String expression = '*',
  String? afterWhere,
}) async {
  final String whereClause =
      afterWhere != null ? ' WHERE ${_sanitizeWhere(afterWhere)}' : '';
  final List<Map<String, Object?>> rows = await (await db).transaction(
    (txn) => txn.rawQuery(
      'SELECT count($expression) AS countElement '
      'FROM ${T.toString()}$whereClause',
    ),
  );
  return (rows.first['countElement'] as int?) ?? 0;
}