Skip to content

Getting Started

Bruce Santier edited this page Feb 18, 2025 · 2 revisions

Installation

First, add Fling to your project:

$ dart pub add fling_units

...or add it to your pubspec.yaml manually.

Refresh your dependencies:

$ dart pub get

Import it where you're going to use it:

import 'package:fling_units/fling_units.dart';

First Steps

Measurements

Start out by choosing the unit you want to work with, and instantiate a measurement using whichever way feels most natural (either "calling" the unit, or using an extension on num):

var measurement = 4.centi.meters;
    // or
var measurement = centi.meters(4);

You can perform basic mathematical operations on measurements in a natural way:

measurement * 2;
    // 8.0 cm

measurement + 2.inches;
    // 9.08 cm

measurement ~/ 3.milli.meters;
    // 13

// ... and more ...

Conversion

You can convert the unit a measurement uses to any other unit of the same "dimension" (in our case, distance):

measurement.butAs(inches);
    // 1.574803 in

You can get the value of the measurement by reading its defaultValue, or by converting it to a specific unit:

measurement.defaultValue;
    // 4.0

measurement.as(centi.meters);
    // 4.0

Precision

You can also specify how precise your measurement is, and the output of any conversions will match that precision (nunber of "significant digits"):

measurement.withPrecision(2).butAs(inches);
    // 1.6 in

Derived Units

You can create simple derived units, and by extension derived measurements, via the per and by methods, again either via "calling" or using num extensions:

var speed = 10.meters.per.second;
var surfaceArea = kilo.meters.by.kilo.meters(25);

If you already have measurements for the component parts, you can also use those via over and dot. Fling will calculate the result for you:

var speed = 20.meters.over(2.seconds);
    // 10.0 m⋅s⁻¹

Areas and volumes can be defined using square and cubic shortcuts:

var surfaceArea = square(kilo.meters)(25);
var volume = 9.cubic(centi.meters);

You can convert derived measurements using any similar unit:

speed.butAs(inches.per.minute).withPrecision(3);
    // 23600.0 in⋅min⁻¹

Next Steps

This is a good start, but there is still a long way to go.

  • Read up on the terminology Fling uses.
  • Check the cookbook for more complicated examples and patterns you can follow.
Clone this wiki locally