gpt4 book ai didi

flutter - Flutter 中的设备国家/地区

转载 作者:行者123 更新时间:2023-12-03 13:28:57 28 4
gpt4 key购买 nike

我正在尝试在 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/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com