Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Device Country in Flutter

I am trying to get country of device (Android) in Flutter. I used this tutorial, but I think it is the wrong approach for my problem.

Locale myLocale = Localizations.localeOf(context);
print(myLocale.languageCode.toString() + ' ' + myLocale.countryCode.toString());

Based on this, I have couple of questions/issues:

  • I am always getting en US even though I have set device language to Urdu - Pakistan. So what am I missing?
  • I want to display certain app items based on the country the device is in, not based on language as people living in Pakistan with language set to English (US) will get items actually intended for USA based users. So, should I use geoLocation and get longitude and latitude and decide based on that data? Or is there any other simpler approach just to get country of user?

Thanking in anticipation.

like image 581
Aleem Avatar asked Sep 17 '19 15:09

Aleem


People also ask

How can I get country code from country?

Use the Intl. DisplayNames() constructor to get a country name from a country code, e.g. new Intl. DisplayNames(['en'], {type: 'region'}).

How do you know if your location is on flutter?

you can check if the location service is enabled: if (await Permission. locationWhenInUse. serviceStatus.

How do I find the location address in flutter?

To query the current location of the device, simply make a call to the getCurrentPosition() method. For example: import 'package:geolocator/geolocator. dart';Position position = await Geolocator.


1 Answers

Add this 2 library in pubspec.yaml

geolocator: ^5.1.1
geocoder: ^0.2.1

Then Add permission for Location access. Check here

At last call this method where you want.

Future<String> getCountryName() async {
    Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    debugPrint('location: ${position.latitude}');
    final coordinates = new Coordinates(position.latitude, position.longitude);
    var addresses = await Geocoder.local.findAddressesFromCoordinates(coordinates);
    var first = addresses.first;
    return first.countryName; // this will return country name
}
like image 167
Rahul sharma Avatar answered Sep 18 '22 12:09

Rahul sharma