safeParseSize function
Safely parses a Map representation of a size to a Size object.
This function attempts to convert the provided map
to a Size object.
If the map
is null
, missing required keys (width
and height
), or
contains invalid values, a fallback
size is returned instead.
-
Parameters:
-
Returns: A Size object constructed from the
map
if parsing succeeds, or thefallback
size if it fails. -
Example:
safeParseSize({'width': 200, 'height': 100}); // returns Size(200.0, 100.0)
safeParseSize(null); // returns Size.zero (fallback)
safeParseSize({'width': 'abc', 'height': 50}, fallback: Size(10, 10));
// returns Size(10.0, 10.0) (fallback)
Implementation
Size safeParseSize(Map<String, dynamic>? map, {Size fallback = Size.zero}) {
if (map == null) return fallback;
return Size(
safeParseDouble(map['width'], fallback: fallback.width),
safeParseDouble(map['height'], fallback: fallback.height),
);
}