Temperature.parse constructor
Get a Temperature from raw data of value
and unit symbol
.
If symbol
is provided, value can be either num or numeric
String. Otherwise, value
must be a String and last charther must be
validated unit.
If provided parameter does not stastified the requirement, it throw TypeError.
Implementation
factory Temperature.parse({required Object value, String? symbol}) {
double val;
String s;
if (symbol == null) {
if (!(value is String)) {
throw TypeError();
}
final int lastChar = value.length - 1;
s = value[lastChar];
val = double.parse(value.substring(0, lastChar));
} else {
if (value is num) {
val = value.toDouble();
} else if (value is String) {
val = double.parse(value);
} else {
throw TypeError();
}
s = symbol;
}
switch (s) {
case _c:
return Celsius(val);
case _f:
return Fahrenheit(val);
}
throw TypeError();
}