@@ -12,6 +12,13 @@ int? flexibleIntFromJson(dynamic value) {
1212 return null ; // Fallback for other unexpected types
1313}
1414
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+
1522/// Parses a [dynamic] value into a [double] ?.
1623/// Handles `null` , `double` , `int` , and `String` representations.
1724/// An empty string or a string that fails to parse will result in `null` .
@@ -26,6 +33,13 @@ double? flexibleDoubleFromJson(dynamic value) {
2633 return null ; // Fallback for other unexpected types
2734}
2835
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+
2943/// Parses a [dynamic] value into a [bool] ?.
3044/// Handles `null` , `bool` , `String` (e.g., "true", "false", "1", "0", case-insensitive),
3145/// and `int` (1 for true, 0 for false) representations.
@@ -46,6 +60,13 @@ bool? flexibleBoolFromJson(dynamic value) {
4660 return null ; // Fallback for other unexpected types or unhandled values
4761}
4862
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+
4970/// Parses a [dynamic] value into a [String] ?.
5071/// Handles `null` and `String` . Converts other types (like `int` , `double` , `bool` )
5172/// to their string representation using `.toString()` .
@@ -57,6 +78,13 @@ String? flexibleStringFromJson(dynamic value) {
5778 return value.toString ();
5879}
5980
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+
6088/// Parses a [dynamic] value into a [num] ? (int or double).
6189/// Handles `null` , `num` (and its subtypes `int` , `double` ), and `String` representations.
6290/// An empty string or a string that fails to parse will result in `null` .
@@ -173,6 +201,22 @@ List<T> flexibleListNotNullFromJson<T>(
173201 return parsedSingleItem != null ? [parsedSingleItem] : < T > [];
174202}
175203
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+
176220/// Parses comma-separated strings into a list of strings.
177221/// Handles `null` input.
178222/// If input is already a list, its elements are converted to strings.
0 commit comments