The dart:math Toolkit
Use sqrt, pow, min, max, and random numbers.
The dart:math Toolkit is a free Dart Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Math Beyond Operators
Operators cover the basics, but real work needs roots, powers, and randomness. The dart:math library adds these tools. 🧰
Import the Library
Before using its helpers, add an import at the top of your file. One line unlocks everything inside dart:math.
import 'dart:math';
void main() {
// math tools ready
}Square Root
The sqrt function returns the square root of a number. It always gives a double, so sqrt(9) is 3.0.
import 'dart:math';
void main() {
print(sqrt(9)); // 3.0
}Raising to a Power
Use pow to raise a base to an exponent. So pow(2, 10) computes 1024, perfect for scaling and growth math.
import 'dart:math';
void main() {
print(pow(2, 10)); // 1024
}Smallest With min
The min function returns the smaller of two numbers. It is a quick way to clamp a value to an upper limit.
import 'dart:math';
void main() {
print(min(3, 8)); // 3
}Largest With max
Likewise, max returns the larger of two values. Pair it with min to keep numbers inside a safe range.
import 'dart:math';
void main() {
print(max(3, 8)); // 8
}The pi Constant
dart:math gives you pi ready to use, no typing the digits. It makes circle and angle calculations accurate and readable.
import 'dart:math';
void main() {
print(pi); // 3.141592653589793
}Create a Random
Make a Random object to generate unpredictable values. One instance can produce as many random numbers as you need.
import 'dart:math';
void main() {
final rng = Random();
}Random Integers
Call nextInt with a bound to get a whole number from zero up to but not including that bound. Great for dice.
import 'dart:math';
void main() {
print(Random().nextInt(6) + 1); // 1..6
}Random Doubles
Use nextDouble for a fractional value between 0.0 and 1.0. Scale it by multiplying to fit any range you want.
import 'dart:math';
void main() {
print(Random().nextDouble()); // 0.0..1.0
}Absolute Value
Numbers also carry their own helper: abs strips the sign and returns the magnitude, turning -7 into 7 instantly.
void main() {
print((-7).abs()); // 7
}Quick Check
One last check on the math toolkit.
Recap
With dart:math you get sqrt, pow, min, max, pi, and Random. Import once and your number toolkit is complete. 🎉
Frequently asked questions
Is the “The dart:math Toolkit” lesson free?
Yes — the full text of “The dart:math Toolkit” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “The dart:math Toolkit”?
Use sqrt, pow, min, max, and random numbers. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Dart Academy?
No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The dart:math Toolkit” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Dart Academy lesson?
Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.