我正在尝试在 Flutter 中获取设备国家/地区(Android)。我用过 this tutorial ,但我认为这是解决我问题的错误方法。
Locale myLocale = Localizations.localeOf(context);
print(myLocale.languageCode.toString() + ' ' + myLocale.countryCode.toString());
基于此,我有几个问题/问题:
en US
即使我已将设备语言设置为乌尔都语 - 巴基斯坦。那么我错过了什么? geoLocation
并获得经度和纬度并根据该数据做出决定?或者有没有其他更简单的方法来获取用户所在的国家/地区? 期待中的感谢。
最佳答案
在 pubspec.yaml
中添加这 2 个库
geolocator: ^5.1.1
geocoder: ^0.2.1
然后为 Location
添加权限使用权。查询 here最后在你想要的地方调用这个方法。
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
}
关于flutter - Flutter 中的设备国家/地区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57977167/