diff --git a/objectbox/lib/src/native/query/query.dart b/objectbox/lib/src/native/query/query.dart index 067fa32f..ecf6dea6 100644 --- a/objectbox/lib/src/native/query/query.dart +++ b/objectbox/lib/src/native/query/query.dart @@ -540,6 +540,34 @@ abstract class Condition { } } +/// Extension on nullable [Condition] to simplify building queries +/// where conditions are accumulated conditionally. +extension NullableCondition on Condition? { + /// Combines this condition with [other] using AND. + /// If this is null, returns [other] directly. + /// + /// Useful when building queries where conditions are applied selectively: + /// ```dart + /// Condition? condition; + /// condition = condition.and(City_.district.equals(id)); + /// condition = condition.and(City_.name.contains(name)); + /// query = box.query(condition).build(); + /// ``` + Condition and(Condition other) => this?.and(other) ?? other; + + /// Combines this condition with [other] using OR. + /// If this is null, returns [other] directly. + /// + /// Useful when building queries where conditions are applied selectively: + /// ```dart + /// Condition? condition; + /// condition = condition.or(City_.district.equals(id)); + /// condition = condition.or(City_.name.contains(name)); + /// query = box.query(condition).build(); + /// ``` + Condition or(Condition other) => this?.or(other) ?? other; +} + class _NullCondition extends Condition { final QueryProperty _property; final _ConditionOp _op;