convert static method

Future<double?> convert({
  1. required Currency from,
  2. required Currency to,
  3. required double amount,
  4. bool withoutRounding = false,
})

Implementation

static Future<double?> convert({
  required Currency from,
  required Currency to,
  required double amount,
  bool withoutRounding = false,
}) async {
  try {
    String url =
        "${ApiService.ENDPOINT}${from == Currency.turkisL ? 'try' : from.name}.json";
    double value = 0.0;

    /// get the latest currency rate
    Response? resp = (await ApiService.getConvertedAmount(url));
    if (resp != null) {
      double unitValue = double.parse(
          jsonDecode(resp.body)[from == Currency.turkisL ? 'try' : from.name]
                  [to.name]
              .toString());
      value = amount * unitValue;
    }
    if (withoutRounding) {
      return double.parse(value.toString());
    }
    return double.parse(value.toStringAsFixed(2));
  } catch (err) {
    if (kDebugMode) {
      print("convert err $err");
    }
    return 0.0;
  }
}