Flutter功能扩展插件fixer_sdk的使用

Flutter功能扩展插件fixer_sdk的使用

Fixer Unofficial SDK

通过此插件可以获取货币汇率并进行货币转换。
由Radamés J. Valentín Reyes在波多黎各制作。


获取API密钥

请订阅并从这里获取您的API密钥。


导入插件

pubspec.yaml文件中添加依赖项:

dependencies:
  fixer_sdk: ^版本号

然后在代码中导入:

import 'package:fixer_sdk/fixer_sdk.dart';

示例代码

获取最新汇率

以下代码展示了如何使用fixer_sdk插件获取最新的汇率数据:

void main() async {
  // 替换为您的API密钥
  String apiKey = "您的API密钥";

  try {
    // 获取最新的汇率数据
    ExchangeRates rates = await FixerSDK(apikey: apiKey).getLatestRates();

    // 打印汇率信息
    print("最新汇率数据: $rates");
  } catch (e) {
    // 捕获错误并打印
    print("获取汇率失败: $e");
  }
}

货币转换

以下代码演示了如何将一种货币转换为另一种货币:

void main() async {
  // 替换为您的API密钥
  String apiKey = "您的API密钥";

  try {
    // 获取最新的汇率数据
    ExchangeRates rates = await FixerSDK(apikey: apiKey).getLatestRates();

    // 定义要转换的货币
    Currency fromCurrency = Currency(amount: 15, currencyCode: "USD");

    // 目标货币代码
    String toCurrencyCode = "CAD";

    // 进行货币转换
    Currency converted = FixerSDK.equivalentIn(
      fromCurrency: fromCurrency,
      toCurrency: toCurrencyCode,
      rates: rates,
    );

    // 输出转换结果
    print("${fromCurrency.amount} ${fromCurrency.currencyCode} = ${converted.amount} $toCurrencyCode");
  } catch (e) {
    // 捕获错误并打印
    print("货币转换失败: $e");
  }
}

更多关于Flutter功能扩展插件fixer_sdk的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter功能扩展插件fixer_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


fixer_sdk 是一个用于在 Flutter 应用中获取外汇汇率数据的插件。它基于 Fixer API,提供了简单易用的接口来获取最新的汇率信息。以下是如何在 Flutter 项目中使用 fixer_sdk 的详细步骤。

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 fixer_sdk 依赖:

dependencies:
  flutter:
    sdk: flutter
  fixer_sdk: ^1.0.0  # 请检查最新版本

然后运行 flutter pub get 来安装依赖。

2. 获取 API 密钥

在使用 fixer_sdk 之前,你需要在 Fixer 网站上注册并获取一个 API 密钥。

3. 初始化 SDK

在你的 Flutter 应用中,首先需要初始化 Fixer 实例,并传入你的 API 密钥。

import 'package:fixer_sdk/fixer_sdk.dart';

void main() {
  Fixer.initialize(apiKey: 'YOUR_API_KEY');
  runApp(MyApp());
}

4. 获取汇率数据

你可以使用 Fixer 实例来获取最新的汇率数据。以下是一些常见的用法:

获取所有货币的汇率

void fetchLatestRates() async {
  try {
    final rates = await Fixer.getLatestRates();
    print(rates);
  } catch (e) {
    print('Error fetching rates: $e');
  }
}

获取特定货币的汇率

void fetchSpecificRates() async {
  try {
    final rates = await Fixer.getLatestRates(symbols: ['USD', 'EUR']);
    print(rates);
  } catch (e) {
    print('Error fetching rates: $e');
  }
}

获取历史汇率

void fetchHistoricalRates() async {
  try {
    final rates = await Fixer.getHistoricalRates(date: '2023-01-01');
    print(rates);
  } catch (e) {
    print('Error fetching historical rates: $e');
  }
}

5. 处理响应数据

Fixer 返回的数据通常是一个 Map<String, dynamic>,其中包含汇率信息。你可以根据需要解析和使用这些数据。

void fetchLatestRates() async {
  try {
    final rates = await Fixer.getLatestRates();
    print('Base Currency: ${rates['base']}');
    print('Rates: ${rates['rates']}');
  } catch (e) {
    print('Error fetching rates: $e');
  }
}

6. 错误处理

在使用 fixer_sdk 时,可能会遇到各种错误,例如网络问题、API 密钥无效等。你可以使用 try-catch 块来捕获并处理这些错误。

void fetchLatestRates() async {
  try {
    final rates = await Fixer.getLatestRates();
    print(rates);
  } on FixerException catch (e) {
    print('Fixer API error: ${e.message}');
  } catch (e) {
    print('Unexpected error: $e');
  }
}

7. 其他功能

fixer_sdk 还提供了其他一些功能,例如获取支持的货币列表、转换货币等。你可以参考官方文档或源代码来了解更多信息。

8. 示例代码

以下是一个完整的示例,展示了如何在 Flutter 应用中使用 fixer_sdk 获取并显示最新的汇率数据。

import 'package:flutter/material.dart';
import 'package:fixer_sdk/fixer_sdk.dart';

void main() {
  Fixer.initialize(apiKey: 'YOUR_API_KEY');
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Fixer SDK Example'),
        ),
        body: Center(
          child: RateDisplay(),
        ),
      ),
    );
  }
}

class RateDisplay extends StatefulWidget {
  [@override](/user/override)
  _RateDisplayState createState() => _RateDisplayState();
}

class _RateDisplayState extends State<RateDisplay> {
  Map<String, dynamic>? rates;

  [@override](/user/override)
  void initState() {
    super.initState();
    fetchLatestRates();
  }

  void fetchLatestRates() async {
    try {
      final data = await Fixer.getLatestRates();
      setState(() {
        rates = data['rates'];
      });
    } catch (e) {
      print('Error fetching rates: $e');
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return rates == null
        ? CircularProgressIndicator()
        : ListView(
            children: rates!.entries.map((entry) {
              return ListTile(
                title: Text(entry.key),
                subtitle: Text(entry.value.toString()),
              );
            }).toList(),
          );
  }
}
回到顶部