fling_units 2.2.7 fling_units: ^2.2.7 copied to clipboard
Dependency-free, type-safe Dart unit conversion library. Decouple specific unit types you need to work with from the rest of your code.
import 'package:fling_units/fling_units.dart';
void main() {
// Create an instance of the measurement you care about.
// You may use any of several construction methods.
// Include the precision of your measurement for best results!
var bodyTemperature = Temperature.ofFahrenheit(93.4);
var distanceToSeattle = Distance.sum(
[
miles(123),
yards(15),
feet(2),
inches(4),
],
precision: Precision(3),
);
var distanceToTheMoon = kilo.meters(382500, precision: Precision(4));
var depthsOfMyMind = Volume.infinite();
//------------------------------------------------//
// Perform basic arithmetic on compatible units.
var distanceToSeattleAndBack = distanceToSeattle * 2;
var distanceToSeattleIfYouForgotSomethingAtHome =
(distanceToSeattleAndBack + distanceToSeattle)
.withPrecision(Precision(6));
//------------------------------------------------//
// Compare measurements of the same type (e.g. Distance or Temperature).
if (distanceToTheMoon > distanceToSeattle) {
print('Whew, we are still in Kansas.');
} else {
print("I don't think we're in Kansas any more.");
}
print('\nIt is cold...');
while (bodyTemperature < Temperature.ofFahrenheit(98.6)) {
print('I need another blanket...');
bodyTemperature += fahrenheit(2);
}
print('Ahh, much better!');
// Attempting to compare incompatible types is a compile-time error.
// if (bodyTemperature <= depthsOfMyMind) { // compile error! }
//------------------------------------------------//
// Inherent ordering of items allows sorting lists with the built-in methods.
var distances = [
inches(1, precision: Precision(3)),
centi.meters(1, precision: Precision(3)),
Distance.zero(),
miles(1, precision: Precision(3)),
feet(-1, precision: Precision(3)),
Distance.negativeInfinite(),
];
print('\nThese are all out of whack: $distances');
distances.sort();
print('Much better: $distances');
//------------------------------------------------//
// When you're ready, interpret the measurement using whatever unit you like.
print(
'\nI drove ${distanceToSeattleIfYouForgotSomethingAtHome.as(yards)} yards because I left my driving glasses at home.');
print(
'I can fit ${depthsOfMyMind.asVolume(Volume.cubic(meters))} boxes of bananas in my mind.');
//------------------------------------------------//
// Some of the more common derived units (e.g. Area) have full syntactic support.
var monitorSurfaceArea = Area.square(inches)(800, precision: Precision(4));
print('\nMy monitor dimensions:');
print('${monitorSurfaceArea.asArea(Area.square(meters))} m²');
print('${monitorSurfaceArea.asArea(Area.square(centi.meters))} cm²');
print('${monitorSurfaceArea.asArea(Area.square(inches))} in²');
print('${monitorSurfaceArea.as(inches, inches)} in² (alternate form)');
print(
'${monitorSurfaceArea.as(inches, centi.meters)} in x cm (in case you ever needed that...)');
// You can also build them from their component parts.
var oneSquareInch = Area.of(
inches(1, precision: Precision(3)),
inches(1, precision: Precision(3)),
);
print('\nOne square inch is '
'${oneSquareInch.asArea(Area.square(feet))} square feet.');
//------------------------------------------------//
// Need a derived unit that isn't specifically implemented? Build it yourself!
// You can also use the common derived units to create your masterpiece.
var fuelConsumption = distanceToSeattle.per(usGallons(2.4));
print('\nDriving to Seattle made me realize how great my fuel economy is!');
print('${fuelConsumption.as(miles, usGallons)} mpg');
// Interpret the derived unit in any combination of component units.
print('${fuelConsumption.as(miles, liters)} mpl');
print('${fuelConsumption.as(kilo.meters, liters)} kpl');
print('${fuelConsumption.as(kilo.meters, usGallons)} kpg');
var coulombs = seconds(4).by(amperes(8));
print('My invention generates $coulombs!');
//------------------------------------------------//
// Want syntactic sugar? Any measurement can be created from a number using
// extensions. We recommend wrapping doubles in parenthesis for readability.
var yourBucketSize = 3.liters;
var myBucketSize = (1.5).deka.liters;
print(
'\nMy bucket is ${myBucketSize.compareMagnitude(yourBucketSize)} times bigger than yours!');
//------------------------------------------------//
// The unit interpreters themselves have a toString() method that will produce
// the "standard" short form of the unit. Measurements will make use of that
// in their own toString() methods using whichever unit was used to
// instantiate them. You can also change the default unit later.
final goldAmount = 1234.milli.grams.withPrecision(Precision(4));
print('I have $goldAmount of gold!');
print('I have ${goldAmount.as(kilo.grams)} ${kilo.grams} of gold!');
print('I have ${goldAmount.withDefaultUnit(ounces)} of gold!');
// This is also true for derived units. The library will produce a default
// unit name, but you can also supply your own.
final carSpeed = DerivedMeasurement<Distance, Time>.divide(
100.miles.withPrecision(Precision(3)),
1.hours.withPrecision(Precision(3)),
);
print('\nMy car is going $carSpeed!');
final carVelocity = carSpeed.withDefaultUnit(DerivedMeasurementInterpreter(
feet, minutes, true, MeasurementPrefix.unit(), 'gizmos per doodad'));
print('My car is going $carVelocity!');
print(
'My car is going ${carVelocity.defaultValue} in ${carVelocity.defaultInterpreter}!');
final lapsPerSecond = (0.00123).hertz;
print('That is $lapsPerSecond (laps per second), or ${lapsPerSecond.as(milli.hertz)} ${milli.hertz}');
// Have fun!
}