@@ -12,6 +12,13 @@ int? flexibleIntFromJson(dynamic value) {
12
12
return null ; // Fallback for other unexpected types
13
13
}
14
14
15
+ /// Parses a [dynamic] value into a non-nullable [int] .
16
+ /// Similar to [flexibleIntFromJson] , but returns 0 instead of null.
17
+ /// Useful when you need to guarantee a non-null int value.
18
+ int flexibleRequiredIntFromJson (dynamic value) {
19
+ return flexibleIntFromJson (value) ?? 0 ;
20
+ }
21
+
15
22
/// Parses a [dynamic] value into a [double] ?.
16
23
/// Handles `null` , `double` , `int` , and `String` representations.
17
24
/// An empty string or a string that fails to parse will result in `null` .
@@ -26,6 +33,13 @@ double? flexibleDoubleFromJson(dynamic value) {
26
33
return null ; // Fallback for other unexpected types
27
34
}
28
35
36
+ /// Parses a [dynamic] value into a non-nullable [double] .
37
+ /// Similar to [flexibleDoubleFromJson] , but returns 0.0 instead of null.
38
+ /// Useful when you need to guarantee a non-null double value.
39
+ double flexibleRequiredDoubleFromJson (dynamic value) {
40
+ return flexibleDoubleFromJson (value) ?? 0.0 ;
41
+ }
42
+
29
43
/// Parses a [dynamic] value into a [bool] ?.
30
44
/// Handles `null` , `bool` , `String` (e.g., "true", "false", "1", "0", case-insensitive),
31
45
/// and `int` (1 for true, 0 for false) representations.
@@ -46,6 +60,13 @@ bool? flexibleBoolFromJson(dynamic value) {
46
60
return null ; // Fallback for other unexpected types or unhandled values
47
61
}
48
62
63
+ /// Parses a [dynamic] value into a non-nullable [bool] .
64
+ /// Similar to [flexibleBoolFromJson] , but returns false instead of null.
65
+ /// Useful when you need to guarantee a non-null boolean value.
66
+ bool flexibleRequiredBoolFromJson (dynamic value) {
67
+ return flexibleBoolFromJson (value) ?? false ;
68
+ }
69
+
49
70
/// Parses a [dynamic] value into a [String] ?.
50
71
/// Handles `null` and `String` . Converts other types (like `int` , `double` , `bool` )
51
72
/// to their string representation using `.toString()` .
@@ -57,6 +78,13 @@ String? flexibleStringFromJson(dynamic value) {
57
78
return value.toString ();
58
79
}
59
80
81
+ /// Parses a [dynamic] value into a non-nullable [String] .
82
+ /// Similar to [flexibleStringFromJson] , but returns an empty string instead of null.
83
+ /// Useful when you need to guarantee a non-null string value.
84
+ String flexibleRequiredStringFromJson (dynamic value) {
85
+ return flexibleStringFromJson (value) ?? '' ;
86
+ }
87
+
60
88
/// Parses a [dynamic] value into a [num] ? (int or double).
61
89
/// Handles `null` , `num` (and its subtypes `int` , `double` ), and `String` representations.
62
90
/// An empty string or a string that fails to parse will result in `null` .
@@ -173,6 +201,22 @@ List<T> flexibleListNotNullFromJson<T>(
173
201
return parsedSingleItem != null ? [parsedSingleItem] : < T > [];
174
202
}
175
203
204
+ /// Alias for [flexibleListNotNullFromJson] to provide a consistent naming convention
205
+ /// with other required functions.
206
+ /// Returns an empty list if the input is null or if parsing results in all nulls.
207
+ ///
208
+ /// Example:
209
+ /// ```dart
210
+ /// @JsonKey(fromJson: (v) => flexibleRequiredListFromJson(v, flexibleIntFromJson))
211
+ /// List<int> scores; // Guarantees a List<int>, never null.
212
+ /// ```
213
+ List <T > flexibleRequiredListFromJson <T >(
214
+ dynamic value,
215
+ T ? Function (dynamic ) itemParser,
216
+ ) {
217
+ return flexibleListNotNullFromJson <T >(value, itemParser);
218
+ }
219
+
176
220
/// Parses comma-separated strings into a list of strings.
177
221
/// Handles `null` input.
178
222
/// If input is already a list, its elements are converted to strings.
0 commit comments