SimpleCustomConversion constructor
SimpleCustomConversion(})
Class for simple custom conversions. E.g.:
final Map<String, double> conversionMap = {
'EUR': 1,
'USD': 1.2271,
'GBP': 0.9033,
'JPY': 126.25,
'CNY': 7.9315,
};
final Map<String, String> mapSymbols = {
'EUR': '€',
'USD': '\$',
'GBP': '₤',
'JPY': '¥',
'CNY': '¥',
};
var customConversion = SimpleCustomConversion(conversionMap, mapSymbols: mapSymbols);
customConversion.convert('EUR', 1);
Unit usd = customConversion.getUnit('USD');
print('1€ = ${usd.stringValue}${usd.symbol}');
Implementation
SimpleCustomConversion(this.mapConversion,
{this.mapSymbols,
this.significantFigures = 10,
this.removeTrailingZeros = true,
this.useScientificNotation = true,
name}) {
assert(mapConversion.containsValue(1),
'One conversion coefficient must be 1, this will considered the base unit');
if (mapSymbols != null) {
for (var val in mapConversion.keys) {
assert(mapSymbols!.keys.contains(val),
'The key of mapConversion must be the same key of mapSymbols');
}
}
this.name = name;
size = mapConversion.length;
var baseUnit = mapConversion.keys.firstWhere(
(element) => mapConversion[element] == 1); //take the base unit
List<Node> leafNodes = [];
mapConversion.forEach((key, value) {
if (key != baseUnit) {
//I'm just interested in the relationship between the base unit and the other units.
leafNodes.add(Node(name: key, coefficientProduct: 1 / value));
}
});
if (mapSymbols == null) {
mapSymbols = <dynamic, String?>{};
for (var val in mapConversion.keys) {
mapSymbols![val] = null;
}
}
Node conversionTree = Node(name: baseUnit, leafNodes: leafNodes);
_customConversion = CustomConversion(
conversionTree: conversionTree,
mapSymbols: mapSymbols!,
significantFigures: significantFigures,
removeTrailingZeros: removeTrailingZeros,
useScientificNotation: useScientificNotation,
name: name ?? PROPERTY.angle);
}