如何按国家/地区代码获取货币和货币符号? [英] How to get currency and currency symbol in flutter by country code?

查看:99
本文介绍了如何按国家/地区代码获取货币和货币符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的应用程序从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
}

这篇关于如何按国家/地区代码获取货币和货币符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆