Skip to content
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

Merged
merged 6 commits into from Dec 13, 2021

Conversation

M123-dev
Copy link
Member

No description provided.

lib/utils/CountryHelper.dart Outdated Show resolved Hide resolved
Comment on lines 1011 to 1014
static OpenFoodFactsCountry? fromJson(String? code) =>
OpenFoodFactsCountry.values.firstWhere(
(final OpenFoodFactsCountry country) => country.iso2Code == code,
);
Copy link
Contributor

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).

Copy link
Member Author

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.

Copy link
Contributor

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 extensions.

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;
  }
}

Copy link
Contributor

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;
  }
}

Copy link
Member Author

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>
Copy link
Contributor

@monsieurtanuki monsieurtanuki left a 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!

@M123-dev
Copy link
Member Author

Thanks @monsieurtanuki

@M123-dev M123-dev merged commit 8b6c9bc into master Dec 13, 2021
@M123-dev M123-dev deleted the country-from-cc branch December 13, 2021 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants