New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Allow to get OpenFoodFactsCountry from countryCode #314
Conversation
lib/utils/CountryHelper.dart
Outdated
static OpenFoodFactsCountry? fromJson(String? code) => | ||
OpenFoodFactsCountry.values.firstWhere( | ||
(final OpenFoodFactsCountry country) => country.iso2Code == code, | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does not work for an unknown code
: crashes, instead of an expected null
.
firstWhere
is not adequate, as it never returns null.
Your previous syntax was correct (it's just that the return;
was useless).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I know that it theoretically worked but it didn't make sense since the extension only worked for specific values of the enum, not for the enum itself, so there you would have to write OpenFoodFactsCountry.FRANCE.fromCountryCode('de')
which would return germany, that's why I moved it and now the structure is similar to the one in the OpenFoodFactsLanguage.
Regarding the null problem, it's not possible to return null from orElse
so we could either add a UNKNOW value to the enum as in the OpenFoodFactsLanguage but I would rather just switch back to the old forEach loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, static
methods don't seem to be allowed in extension
s.
Similarly to what you wrote initially, and which seems to work:
class CountryHelper {
static OpenFoodFactsCountry? fromJson(String? code) {
OpenFoodFactsCountry? result;
OpenFoodFactsCountry.values.forEach((final OpenFoodFactsCountry key) {
if (key.iso2Code == code) {
result = key;
}
});
return result;
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or even better (I don't find the dart map methods always 100% relevant):
class CountryHelper {
static OpenFoodFactsCountry? fromJson(String? code) {
if (code == null) {
return null;
}
for (final OpenFoodFactsCountry key in OpenFoodFactsCountry.values) {
if (key.iso2Code == code) {
return key;
}
}
return null;
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed it, good suggestion
Co-Authored-By: monsieurtanuki <11576431+monsieurtanuki@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be OK now @M123-dev!
Thanks @monsieurtanuki |
No description provided.