convert method

  1. @override
void convert(
  1. NUMERAL_SYSTEMS name,
  2. String? value
)
override

Converts a unit with a specific name (e.g. NUMERAL_SYSTEMS.decimal) and value to all other units

Implementation

@override
void convert(NUMERAL_SYSTEMS name, String? value) {
  // if the value is null also the others units are null, this is convenient
  // in order to delete all the other units value, for example in a unit
  // converter app (such as Converter NOW)
  if (value == null) {
    for (Unit unit in _unitList) {
      unit.value = null;
      unit.stringValue = null;
    }
    return;
  }

  const Map<NUMERAL_SYSTEMS, int> bases = {
    NUMERAL_SYSTEMS.decimal: 10,
    NUMERAL_SYSTEMS.hexadecimal: 16,
    NUMERAL_SYSTEMS.octal: 8,
    NUMERAL_SYSTEMS.binary: 2,
  };

  _unitList.singleWhere((e) => e.name == name).stringValue = value;
  if (name == NUMERAL_SYSTEMS.decimal) {
    for (var base in bases.keys.where((e) => e != NUMERAL_SYSTEMS.decimal)) {
      _unitList.singleWhere((e) => e.name == base).stringValue =
          decToBase(value, bases[base]!);
    }
  } else {
    final decimal = baseToDec(value, bases[name]!);
    _unitList
        .singleWhere((e) => e.name == NUMERAL_SYSTEMS.decimal)
        .stringValue = decimal;
    for (var base in bases.keys
        .where((e) => e != NUMERAL_SYSTEMS.decimal && e != name)) {
      _unitList.singleWhere((e) => e.name == base).stringValue =
          decToBase(decimal, bases[base]!);
    }
  }
}