byField method Null safety

List<WhereCondition> byField(
  1. String fieldName,
  2. List<WhereCondition>? conditions
)

Recursively find conditions that evaluate a specific field. A field is a member on a model, such as myUserId in final String myUserId. If the use case for the field only requires one result, say id or primaryKey, firstByField may be more useful.

Implementation

static List<WhereCondition> byField(String fieldName, List<WhereCondition>? conditions) {
  final flattenedConditions = <WhereCondition>[];

  /// recursively flatten all nested conditions
  void expandConditions(WhereCondition condition) {
    if (condition.conditions == null || condition.conditions!.isEmpty) {
      flattenedConditions.add(condition);
    } else {
      condition.conditions?.forEach(expandConditions);
    }
  }

  conditions?.forEach(expandConditions);

  return flattenedConditions.where((c) => c.evaluatedField == fieldName).toList();
}