getListByField method

Future<List<E>> getListByField(
  1. String fieldName,
  2. String fieldValue, {
  3. bool like = false,
  4. int offset = 0,
  5. int limit = 20,
  6. String? orderBy,
  7. SortDirection sortDirection = SortDirection.asc,
})

Perform a query with a where clause fieldName = fieldValue unles the like is true in which case we perform fieldName like fieldValue.

You can do pagination by passing offset and limit.

Sort the results by passing orderBy and sortDirection

Implementation

Future<List<E>> getListByField(String fieldName, String fieldValue,
    {bool like = false,
    int offset = 0,
    int limit = 20,
    String? orderBy,
    SortDirection sortDirection = SortDirection.asc}) async {
  var sql = 'select * from $_tablename ';
  if (like) {
    sql += 'where `$fieldName` like ? ';
  } else {
    sql += 'where `$fieldName` = ? ';
  }

  if (orderBy != null) {
    sql += 'order by $orderBy ${sortDirection.name} ';
  }

  sql += 'limit $offset, $limit ';

  return query(sql, [fieldValue]);
}