genderFromJson function
Converts a JSON gender string to a corresponding Gender enumeration.
This function takes a JSON gender string as input and converts it to the
corresponding Gender
enumeration. If the input string is null, empty,
or matches the constant ''
, the function returns the default gender
Gender.unknown
. Otherwise, it searches through the Gender
enum values
and compares their lowercase names with the lowercase input string to find
a match. The first matching Gender
enum is returned. If no match is found,
an exception will be thrown.
@param genderString The JSON gender string to convert to a Gender
enum.
@return A Gender
enum corresponding to the input gender string.
Implementation
Gender genderFromJson(String? genderString) {
if (genderString == null || genderString.isEmpty || genderString == '') {
return Gender.unknown;
}
return Gender.values.where((Gender gender) {
return gender.name.toLowerCase() == genderString.toLowerCase();
}).first;
}