miguelpruivo / country_codes

Country codes is an helper package that provides country details given a particular localization, such as dial codes, ISO 3166 codes (alpha-2, alpha-3) and country names.
MIT License
29 stars 34 forks source link

Library crashes when providing app-specific locale #1

Closed kightsonsanom closed 4 years ago

kightsonsanom commented 4 years ago

Hello,

I am trying to get country code based on the locale of the application, here is how I am invoking the method:

    final localeCodes = CountryCodes.detailsForLocale(Localizations.localeOf(context));

Following throws this error:

image

This is caused because countryCode is an optional parameter in Locale and might be null, here is the locale constructor: image

and here is how the locale is resolved in the library

image

miguelpruivo commented 4 years ago

Hi @kightsonsanom, this is not actually a bug with the plugin itself but rather from iOS simulator that you might be using with it.

For some reason, iOS simulators don't have a default country set (this doesn't ever happen with real devices btw) and you'll need to go to settings > general > language & region > region > select one, even if it's already selected. After that you should be fine.

Anyway, I've updated the assert to also make sure that countryCode is not null and provide this same information to the developer as it may be missed at first.

Let me know if it worked for you.

Thanks!

kightsonsanom commented 4 years ago

Hi @miguelpruivo thank you for the quick answer. The issue above happened to me on an Android emulator but I also checked on physical Pixel 2 XL and OnePlus 6 and it also happens there. For some reason, countryCode is still null.

What about accessing CountryDetails map keys by languageCode instead? This property is a required one on Locale object.

miguelpruivo commented 4 years ago

@kightsonsanom I'm afraid that every device should have a country associated because the language tag must be from a country. This is, en can be a language tag from many countries, and this could result in wrong information being provided. How could one tell if en is from US or GB, for example?

I'm surprised that in Android devices you are experiencing this. Might have to investigate this. At first, this was only an issue with iOS simulators.

kightsonsanom commented 4 years ago

I agree that the country code is necessary for this case. If you find yourself not able to reproduce the issue I can provide more details. Thanks!

miguelpruivo commented 4 years ago

@kightsonsanom yes, that would be great. How do you manage to replicate it consistently?

kightsonsanom commented 4 years ago

I am actually doing it the same way it is done in the package README :

  1. Initialize the plugin: image

  2. Use it on the first screen of the application. I use it in didChangeDependencies not initState because of Localizations Inherited widget, but in the build method the result is the same.

image

Here I think there is a mistake in plugin Readme, detailsForLocale is a method defined in CountryCodes not CountryDetails class but its not that important. Also in Readme, there is Localization instead if Localizations, is that a typo too? Also Readme fragment for the reference.

image

kightsonsanom commented 4 years ago

For some reason, iOS simulators don't have a default country set (this doesn't ever happen with real devices btw) and you'll need to go to settings > general > language & region > region > select one, even if it's already selected. After that you should be fine.

@miguelpruivo I was able to reproduce this one as well. It happens on iOS simulator when we request device-specific locale like this final localeCodes = CountryCodes.detailsForLocale();

In such case setting region in the settings solves this particular problem but it is different than than the issue of this topic.

miguelpruivo commented 4 years ago

Added default Locale to en-US on 1.0.1 whenever device hasn't one — although this is likely to only happen on iOS simulators.