From f09d1f215048e7823a6eb6ad969f3df99122f13b Mon Sep 17 00:00:00 2001 From: Nickolas de Luca Alberton Date: Thu, 11 Jun 2026 14:24:27 -0300 Subject: [PATCH] Add NullableCondition extension for Condition Introduce a NullableCondition extension on Condition? providing and(Condition) and or(Condition) helpers. These methods return the provided condition when the receiver is null, or delegate to the existing and/or logic otherwise, simplifying conditional accumulation of query conditions and removing boilerplate null checks. Doc examples included to demonstrate usage when building queries. --- objectbox/lib/src/native/query/query.dart | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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;