Skip to content

Commit d1d2b7f

Browse files
committed
Add Value.absentIfNull
1 parent fe0df6d commit d1d2b7f

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

drift/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
- Improve stack traces for errors happening on drift isolates (which includes
1313
usages of `NativeDatabase.createInBackground`).
1414
- Don't cache `EXPLAIN` statements, avoiding schema locks.
15+
- Deprecate `Value.ofNullable` in favor of `Value.absentIfNull`, which is more
16+
explicit about its behavior and allows nullable types too.
1517
- Migrate `WasmDatabase` to `dart:js_interop` and `package:web`.
1618

1719
## 2.15.0

drift/lib/src/runtime/data_class.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ class Value<T> {
158158
/// This constructor should only be used when [T] is not nullable. If [T] were
159159
/// nullable, there wouldn't be a clear interpretation for a `null` [value].
160160
/// See the overall documentation on [Value] for details.
161+
@Deprecated('Use Value.absentIfNull instead')
161162
const Value.ofNullable(T? value)
162163
: assert(
163164
value != null || null is! T,
@@ -167,6 +168,15 @@ class Value<T> {
167168
_value = value,
168169
present = value != null;
169170

171+
/// Create a value that is absent if [value] is `null` and [present] if it's
172+
/// not.
173+
///
174+
/// The functionality is equiavalent to the following:
175+
/// `x != null ? Value(x) : Value.absent()`.
176+
const Value.absentIfNull(T? value)
177+
: _value = value,
178+
present = value != null;
179+
170180
@override
171181
String toString() => present ? 'Value($value)' : 'Value.absent()';
172182

drift/test/database/data_class_test.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,16 @@ void main() {
105105
expect(entry.toCompanion(true), const PureDefaultsCompanion());
106106
});
107107

108-
test('nullable values cannot be used with nullOrAbsent', () {
108+
test('utilities to wrap nullable values', () {
109109
expect(
110-
// ignore: prefer_const_constructors
110+
// ignore: prefer_const_constructors, deprecated_member_use_from_same_package
111111
() => Value<int?>.ofNullable(null),
112112
throwsA(isA<AssertionError>()));
113113

114-
expect(const Value<int>.ofNullable(null).present, isFalse);
115-
expect(const Value<int?>.ofNullable(12).present, isTrue);
116-
expect(const Value<int>.ofNullable(23).present, isTrue);
114+
expect(const Value<int?>.absentIfNull(null).present, isFalse);
115+
expect(const Value<int>.absentIfNull(null).present, isFalse);
116+
expect(const Value<int?>.absentIfNull(12).present, isTrue);
117+
expect(const Value<int>.absentIfNull(23).present, isTrue);
117118
});
118119

119120
test('companions support hash and equals', () {

0 commit comments

Comments
 (0)