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

标签 flutter country-codes

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

    相关文章:

    flutter - 我如何在Flutter中将数据发送到文本字段

    flutter - 如何在 ListView 构建器上应用异步操作?

    dart - Flutter tween 基本动画在 `FutureBuilder` 内部不起作用

    java - 国家/地区本地 TZD 格式的纪元时间戳

    java - 在 Android 平板电脑上调用 getSimCountryIso() 时会发生什么?

    svg - 在哪里可以找到使用 2 位或 3 位国家代码的国家/地区的 SVG 或 GeoJSON?

    flutter - TextFormField中onChanged()的setState()可以异步吗?

    macos - Flutter 在代理 : Proxy failed to establish tunnel (407 Proxy Authentication Required) 后面失败

    browser - 'de' 和 'de-DE' 语言代码有什么区别?

    java - 获取完整的联系人电话号码和国家/地区代码,即使未输入国家/地区代码