From 0eb077db34fef8f987bb01ff7f8c2df4f4b574fe Mon Sep 17 00:00:00 2001 From: Pascal Welsch Date: Fri, 30 Aug 2024 11:49:23 +0200 Subject: [PATCH] Allow letOrNull to return R? --- lib/src/pick_let.dart | 2 +- test/src/pick_let_test.dart | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/src/pick_let.dart b/lib/src/pick_let.dart index 9e82e8e..69168ef 100644 --- a/lib/src/pick_let.dart +++ b/lib/src/pick_let.dart @@ -59,7 +59,7 @@ extension NullableLet on Pick { /// user = User.fromJson(pick.asMap()); /// } /// ``` - R? letOrNull(R Function(RequiredPick pick) block) { + R? letOrNull(R? Function(RequiredPick pick) block) { if (value == null) return null; return block(required()); } diff --git a/test/src/pick_let_test.dart b/test/src/pick_let_test.dart index 7ed3866..b8dfb76 100644 --- a/test/src/pick_let_test.dart +++ b/test/src/pick_let_test.dart @@ -45,6 +45,10 @@ void main() { Person(name: 'John Snow'), ); expect(nullPick().letOrNull((pick) => Person.fromPick(pick)), isNull); + // allow lambda to return null + final String? a = pick('a').letOrNull((pick) => null); + expect(a, isNull); + expect(pick('a').letOrNull((pick) => null), isNull); expect( () => pick('a').letOrNull((pick) => Person.fromPick(pick)), throwsA(