如何在Flutter中获取当前国家代码

15

我已经尝试通过这个链接获取当前国家代码。 请给我最好的解决方案。

我想在启动页上显示国旗,所以我需要知道用户的国家,这对我很重要。

请帮帮我吧。


我认为你偏爱的解决方案是最好的方式。 - Parth Bhanderi
尝试过了,但是没有得到结果。:( - Akshay Jaiswal
请检查我下面的答案。 - Parth Bhanderi
请查看此线程:https://dev59.com/QFUL5IYBdhLWcg3wDUa2#50924050 - Baig
8个回答

20

够了

WidgetsBinding.instance.window.locale.countryCode

1
如果我想要拨号码而不是国家代码怎么办? - Arslan Kaleem
1
@ArslanKaleem,然后有一个包含所有国家详细信息的地图文件。请参见 https://github.com/natintosh/intl_phone_number_input/blob/develop/lib/src/models/country_list.dart - Pierre
这是干净的好代码。 - dontknowhy
已被弃用,请改用PlatformDispatcher.instance.locale - undefined

9
你可以使用系统区域设置。
final List<Locale> systemLocales = WidgetsBinding.instance.window.locales;
String isoCountryCode = systemLocales.first.countryCode;

1
这应该是被接受的答案,因为它不需要在WidgetAppsupportedLocales 中添加对每个国家/地区代码的显式支持。它只需检索系统的语言配置,而无需使用第三方插件,这也是 OP 所要求的。 - mrcendre

6
从flutter v3.7.0开始,不要使用WidgetsBinding.instance.window.locale.countryCode 'window'已被弃用,不应再使用。通过从上下文中查找当前的FlutterView(通过View.of(context))或直接咨询PlatformDispatcher来代替。此功能在v3.7.0-32.0.pre之后被弃用,以准备即将到来的多窗口支持。
所以你可以在flutter v3.7.0之后使用。
WidgetsBinding.instance.platformDispatcher.locale.countryCode

4
你可以使用以下软件包 devicelocale
在你的pupspec.yaml中添加依赖。
dependencies:
  devicelocale: ^0.2.1

将其导入您想要的类中

import 'package:devicelocale/devicelocale.dart';

那么,您可以使用以下内容获取当前设备的本地化:
String locale = await Devicelocale.currentLocale;

参考资料


3
每次都得到 en_US,这是不正确的,因为我的设备设置为印度。 - Akshay Jaiswal
请提出更好的方法来完成它。 - Akshay Jaiswal
@AkshayJaiswal,你解决了这个问题吗?我也遇到了同样的问题。 - Sameer Donga
@SameerDonga,你有解决这个问题的方法吗? - Chirag Kothiya
@ChiragKothiya 如果您正在使用模拟器设备,则位置始终为美国,您尝试使用真实设备了吗? - LakShan
这将取决于您的设备语言。尝试更改设备语言并检查。 - Kuldeep

2
如果您想获取您当前所在的国家,那么不能依赖于Locale,因为它取决于用户设置。例如,如果您位于哈萨克斯坦,但系统语言是英语(澳大利亚),那么Locale将包含国家代码AU。简单的解决方法是使用此API:http://ip-api.com/json。响应包含您当前所在国家的countryCode
更高级的方法是使用设备定位服务。有一个location包可供使用。

2

只需使用dart内置的Locale类即可获取国家代码和语言。

Locale myLocale = Localizations.localeOf(context);

获取国家代码:
print(myLocale.countryCode);

获取语言代码:
print(myLocale.languageCode);

3
这总是返回en作为国家代码。 - Zilaid

1

这个解决方案已经发布了。我只是创建了一个安全的函数用于获取国家ISO代码。您可以使用systemLocales上的languageCode同时获取语言。

 String getCountryISOCode() {
    final WidgetsBinding? instance = WidgetsBinding.instance;
    if (instance != null) {
      final List<Locale> systemLocales = instance.window.locales;
      String? isoCountryCode = systemLocales.first.countryCode;
      if (isoCountryCode != null) {
        return isoCountryCode;
      } else {
        throw Exception("Unable to get Country ISO code");
      }
    } else {
      throw Exception("Unable to get Country ISO code");
    }
  }

1
您可以使用country_codes包:
安装:
dependencies:
  country_codes: ^2.0.1

使用方法:

await CountryCodes.init(); // Optionally, you may provide a `Locale` to get countrie's localizadName

final Locale deviceLocale = CountryCodes.getDeviceLocale();
print(deviceLocale.languageCode); // Displays en
print(deviceLocale.countryCode); // Displays US

final CountryDetails details = CountryCodes.detailsForLocale();
print(details.alpha2Code); // Displays alpha2Code, for example US.
print(details.dialCode); // Displays the dial code, for example +1.
print(details.name); // Displays the extended name, for example United States.
print(details.localizedName); // Displays the extended name based on device's language (or other, if provided on init)

支持的平台: Android,iOS

enter image description here


无法在Web上运行。抛出MissingPluginException异常。 - Priyshrm
不起作用,在英国时返回美国。 - alfietap

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接