Flutter土耳其里拉汇率查询插件tcmb_api_client的使用
TCMB API 客户端 #
这个 Dart 包提供了一种简单高效的方式来与土耳其共和国中央银行(TCMB)API 进行交互。它允许你获取汇率及其他相关数据。
演示 #
⚠️ 关于数据的信息 #
这是一个用于土耳其共和国中央银行(TCMB)汇率 API 的 Dart 客户端:https://www.tcmb.gov.tr/kurlar/{date}.xml
。你可以使用日期如 today
或 202404/22042024
作为路径参数。请注意,提供的汇率仅供参考,并由土耳其共和国中央银行每日公布,因此它们可能无法反映实时变化。
特性 #
- 获取当前日期的汇率
- 获取特定日期的汇率
- 获取特定货币和日期的单个汇率
- 易于使用,具有干净简单的 API
开始使用 #
要使用此包,请在你的 pubspec.yaml
文件中添加 tcmb_api_client
依赖项。
dependencies:
tcmb_api_client: ^0.0.1
使用方法 #
以下是一个简单的例子,展示如何使用 TcmbApiClient
获取汇率:
import 'package:tcmb_api_client/tcmb_api_client.dart';
Future<List<Currency>> fetchRates() async {
try {
return await _apiClient.getRates();
} catch (e) {
debugPrint(‘Error fetching rates: $e’);
rethrow;
}
}
Future<Currency?> fetchUsdRate() async {
try {
return await _apiClient.getSingleRate(CurrencyCode.USD);
} catch (e) {
debugPrint(‘Error fetching rate: $e’);
rethrow;
}
}
测试和覆盖率 #
此包包含全面的单元测试套件,确保了包功能的可靠性和正确性。 (覆盖率报告目前仅以图像形式添加,稍后将连接到服务)
免责声明 #
请注意,此包与土耳其共和国中央银行(TCMB)无关,也没有正式联系或被其认可。该包独立开发和维护。官方 TCMB 网站可以在 https://www.tcmb.gov.tr 找到。TCMB 及其相关名称、标志、象征和图像是其各自所有者的注册商标。
import 'package:flutter/material.dart';
import 'package:tcmb_api_client/tcmb_api_client.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘TCMB API 客户端演示’,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: ‘TCMB API 客户端演示’),
);
}
}
// 使用 [TcmbApiClient] 从 TCMB API 获取汇率的最小示例
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _apiClient = TcmbApiClient();
late Future<List<Currency>> _ratesFuture;
late Future<Currency?> _usdRateFuture;
@override
void initState() {
super.initState();
_ratesFuture = _fetchRates();
_usdRateFuture = _fetchUsdRate();
}
Future<List<Currency>> _fetchRates() async {
try {
return await _apiClient.getRates();
} catch (e) {
// 处理或根据需要重新抛出错误
debugPrint(‘Error fetching rates: $e’);
rethrow;
}
}
Future<Currency?> _fetchUsdRate() async {
try {
return await _apiClient.getSingleRate(CurrencyCode.USD);
} catch (e) {
// 处理或根据需要重新抛出错误
debugPrint(‘Error fetching rates: $e’);
rethrow;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
actions: [
FutureBuilder<Currency?>(
future: _usdRateFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(child: Text(‘Error: ${snapshot.error}’));
} else {
final usdRate = snapshot.data;
if (usdRate == null) {
return const Center(child: Text(‘无数据’));
}
return Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
‘USD/TRY: ${usdRate.forexBuying}’,
),
);
}
},
),
],
),
body: FutureBuilder<List<Currency>>(
future: _ratesFuture,
builder: (context, snapshot) {
final rates = snapshot.data;
if (rates == null) {
return const Center(child: Text(‘无数据’));
}
return ListView.builder(
itemCount: rates.length,
itemBuilder: (context, index) {
final currency = rates[index];
return ListTile(
title: Text(currency.nameInTurkish),
subtitle: Text(’${currency.code}/TRY’),
trailing: Text(
currency.forexBuying.toString(),
style: Theme.of(context).textTheme.bodyLarge,
),
);
},
);
}),
);
}
@override
void dispose() {
_apiClient.dispose();
super.dispose();
}
}
更多关于Flutter土耳其里拉汇率查询插件tcmb_api_client的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter土耳其里拉汇率查询插件tcmb_api_client的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中集成并使用tcmb_api_client
插件来查询土耳其里拉汇率的示例代码。tcmb_api_client
是一个Flutter插件,用于从土耳其中央银行(TCMB)的API获取汇率数据。
第一步:添加依赖
首先,在pubspec.yaml
文件中添加tcmb_api_client
依赖:
dependencies:
flutter:
sdk: flutter
tcmb_api_client: ^最新版本号 # 请替换为实际的最新版本号
然后运行flutter pub get
来安装依赖。
第二步:导入并使用插件
接下来,在你的Flutter应用中导入并使用这个插件。以下是一个简单的示例,展示如何查询并显示当前的汇率数据。
import 'package:flutter/material.dart';
import 'package:tcmb_api_client/tcmb_api_client.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter TCMB API Client Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late Future<TCMBExchangeRates?> futureExchangeRates;
@override
void initState() {
super.initState();
futureExchangeRates = fetchExchangeRates();
}
Future<TCMBExchangeRates?> fetchExchangeRates() async {
try {
// 假设我们想要获取“Dolar”的汇率,即美元对土耳其里拉的汇率
final result = await TCMBApiClient().getLatestExchangeRates("Dolar");
return result;
} catch (e) {
print("Error fetching exchange rates: $e");
return null;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('TCMB Exchange Rates'),
),
body: Center(
child: FutureBuilder<TCMBExchangeRates?>(
future: futureExchangeRates,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
} else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else if (snapshot.data == null) {
return Text('No data available');
} else {
final exchangeRates = snapshot.data!;
return ListView.builder(
itemCount: exchangeRates.currencies!.length,
itemBuilder: (context, index) {
final currency = exchangeRates.currencies![index];
return ListTile(
title: Text('Currency: ${currency.currencyCode}'),
subtitle: Text('Rate: ${currency.buying} - ${currency.selling}'),
);
},
);
}
},
),
),
);
}
}
代码说明
- 依赖添加:在
pubspec.yaml
文件中添加tcmb_api_client
依赖。 - 导入插件:在需要使用插件的Dart文件中导入
package:tcmb_api_client/tcmb_api_client.dart
。 - 数据获取:在
_MyHomePageState
的initState
方法中调用TCMBApiClient().getLatestExchangeRates("Dolar")
来获取最新的美元对土耳其里拉的汇率数据。 - UI展示:使用
FutureBuilder
来处理异步数据获取,并在UI中展示汇率数据。
这个示例展示了如何集成tcmb_api_client
插件并查询特定货币的汇率数据。你可以根据需要修改代码以查询其他货币的汇率或处理更多复杂的逻辑。