Fluttering Dart

Your Explosive Energy

Use Flutter or Dart to unveil it

Constantin Stan
3 min readJan 3, 2020

--

A while back I’ve started a journey called Fluttering Dart in which I explored Dart programming language fundamentals one must get in touch with, in order to build robust Flutter applications.

It’s a journey that most people with a bit of programming experience can take.

When I got to explore functions in Dart, the first thing that came to my mind to use as an example was Albert Einstein’s famous formula:

E = mc²

It states that the energy (E) can be calculated as the mass (m) multiplied by the speed of light (c) squared.

I started to search for a nice way to present the formula and translate it into a Dart function and stumbled upon this:

The physics and math behind are explained very well in the above article and I’ll be focusing on writing and explaining the equivalent code.

When I first had the idea I was thinking to use DartPad. At that time it supported only the Dart code. Meanwhile, DartPad evolved and now can also run Flutter code which will make things nicer.

Dart

The console Dart only version I wrote when I first had the idea is:

import 'dart:math' as Math;double kTonTNTInJoules = 4184000000000;
double c = 299792458;
int fatMan = 21;
Function E = (double m) => m * Math.pow(c, 2);num joulesToKTonTNT(num val) => val/(kTonTNTInJoules*fatMan);void main() {

double yourCatsWeight = 4.1; //in kg
print('Your cat has ${joulesToKTonTNT(E(yourWeight)).toStringAsFixed(2)} times more explosive…

--

--

Recommended from Medium

Lists

See more recommendations