From 5221e4fdf9ba4e5a74039592d291d1e7cfecdaed Mon Sep 17 00:00:00 2001 From: Rich Date: Wed, 20 Sep 2023 02:59:49 -0500 Subject: [PATCH] Fix IMap.getKey() to return None() when a non-existent key is checked instead of throwing "Null check operator used on a null value". --- lib/src/imap.dart | 14 ++++++++++++-- test/imap_test.dart | 5 +++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/src/imap.dart b/lib/src/imap.dart index 78631af..3d8d464 100644 --- a/lib/src/imap.dart +++ b/lib/src/imap.dart @@ -364,9 +364,19 @@ class _NonEmptyIMapAVLNode extends _IMapAVLNode { if (o == Ordering.EQ) { return some(current._k); } else if (o == Ordering.LT) { - current = current._left._unsafeGetNonEmpty()!; + final l = current._left._unsafeGetNonEmpty(); + if (l != null) { + current = l; + } else { + return none(); + } } else { - current = current._right._unsafeGetNonEmpty()!; + final r = current._right._unsafeGetNonEmpty(); + if (r != null) { + current = r; + } else { + return none(); + } } } return none(); diff --git a/test/imap_test.dart b/test/imap_test.dart index 6d68a06..f6bf759 100644 --- a/test/imap_test.dart +++ b/test/imap_test.dart @@ -60,6 +60,11 @@ void main() { final m = dynamicM as IMap, int>; return m.keys().all((t) => m.getKey(tuple2(t.value1, 0)) == some(t)); })); + qc.check(forall(complexIMaps, (dynamicM) { + final m = dynamicM as IMap, int>; + return m.keys().any((t) => m.getKey(tuple2(t.value1 + 1, 0)) == none()); + })); + }); test("pair iterable", () => qc.check(forall(intIMaps, (dynamicM) {