Skip to content

Commit cd8c1a2

Browse files
committed
Clarify required()
1 parent ec9b15e commit cd8c1a2

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ pick(json).asMapOrEmpty<String, dynamic>();
227227

228228
## Custom parsers
229229

230-
Parsers in `deep_pick` are based on extension functions on the classes `Pick` and `RequiredPick`.
231-
This makes it flexible and easy for 3rd-party types to add custom parers.
230+
Parsers in `deep_pick` are based on extension functions on the classes `Pick`.
231+
This makes it flexible and easy for 3rd-party types to add custom parsers.
232232

233233
This example parses a `int` as Firestore `Timestamp`.
234234
```dart
@@ -290,7 +290,9 @@ final String fullName = pick(data, 'full_name').asStringOrThrow();
290290
final String? level = pick(data, 'level').asIntOrNull();
291291
```
292292

293-
`deep_pick` offers an alternative `required()` API with the same result.
293+
`deep_pick` offers an alternative `required()` API with the same result.
294+
This is useful to make sure a value exists before parsing it.
295+
In case it is `null` or absent a useful error message is printed.
294296

295297
```dart
296298
final String fullName = pick(data, 'full_name').required().asString();
@@ -391,7 +393,7 @@ final milestoneCreator = pick(json, 'milestone', 'creator', 'login').asStringOrT
391393
392394
// Unhandled exception:
393395
// PickException(
394-
// required value at location "milestone" in pick(json, "milestone" (absent), "creator", "login") is absent.
396+
// Expected a non-null value but location "milestone" in pick(json, "milestone" (absent), "creator", "login") is absent.
395397
// Use asStringOrNull() when the value may be null at some point (String?).
396398
// )
397399
```

example/deep_pick_example.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ Future<CounterApiStats> getStats() async {
164164
'keys_updated: $keys_updated, version: "$version"');
165165

166166
// Parse the full object
167-
final CounterApiStats stats = CounterApiStats.fromPick(RequiredPick(json));
167+
final CounterApiStats stats = CounterApiStats.fromPick(pick(json).required());
168168

169169
// Parse lists
170170
final List<CounterApiStats> multipleStats = pick(json, 'items')

0 commit comments

Comments
 (0)