findBy<T extends Model> static method

Future<T?> findBy<T extends Model>({
  1. required String field,
  2. required dynamic value,
})

Finds first record where field == value.

Implementation

static Future<T?> findBy<T extends Model>({
  required String field,
  required dynamic value,
}) async {
  try {
    final constructor = _jsonConstructors[T];
    if (constructor == null) return null;

    final records = await _database.query(
      _getTableName<T>(),
      where: '$field = ?',
      whereArgs: [value],
      limit: 1,
    );

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