如何按国家/地区代码获取货币和货币符号?
问题描述:
我正在将我的应用程序从Java(Android)转换为Flutter(Dart),但找不到从上下文或国家/地区代码中获取货币的方法.
I'm converting my app from Java(Android) to Flutter(Dart) and I can't find a method to get the currency from context or from a country code.
Java代码:
String country = Locale.getDefault().getCountry();
String currency = Currency.getInstance(new Locale("", country)).getCurrencyCode();
Dart中的代码
Locale locale = Localizations.localeOf(context);
String country = locale.countryCode;
答
intl 包起到了作用
import 'package:intl/intl.dart';
void currency() {
Locale locale = Localizations.localeOf(context);
var format = NumberFormat.simpleCurrency(locale: locale.toString());
print("CURRENCY SYMBOL ${format.currencySymbol}"); // $
print("CURRENCY NAME ${format.currencyName}"); // USD
}