find<T extends Model> static method

Future<T?> find<T extends Model>({
  1. required int id,
})

Finds record by primary key (id).

Implementation

static Future<T?> find<T extends Model>({required int id}) async {
  final constructor = _jsonConstructors[T];
  if (constructor == null) return null;

  try {
    final maps = await _database.query(
      _getTableName<T>(),
      where: 'id = ?',
      whereArgs: [id],
      limit: 1,
    );

    if (maps.isNotEmpty) {
      return constructor(maps.first) as T;
    }
    return null;
  } catch (e) {
    print('SQLite find error: $e');
    return null;
  }
}