Flutter: null locale.countryCode on iOS

Issue

This is what I use for Android and iOS

final List<Locale> systemLocales = WidgetsBinding.instance!.window.locales;
String countryCode = WidgetsBinding.instance!.window.locale.countryCode;

On Android it works fine:

enter image description here

But on iOS the country code comes null:

enter image description here

Solution

Solution just for iOS if we cannot get the country code:

Future<String> _getCountryCode() async {
    final link = Uri.parse('http://ip-api.com/json');
    final jsonEncoded = await http.read(link);
    final Map jsonDecoded = json.decode(jsonEncoded);

    return jsonDecoded['countryCode'];
  }

Answered By – Dani

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *