Chapter 3. A Tour of the Dart Libraries

This chapter shows you how to use the major features in Dart’s libraries. It’s just an overview, and by no means comprehensive. Whenever you need more details about a class, consult the Dart API reference.

dart:core—Numbers, Collections, Strings, and More

The Dart core library provides a small but critical set of built-in functionality. This library is automatically imported into every Dart program.

Numbers

The dart:core library defines the num, int, and double classes, which have some basic utilities for working with numbers.

You can convert a string into an integer or double with the parse() methods of int and double, respectively:

assert(int.parse('42') == 42);
assert(int.parse('0x42') == 66);
assert(double.parse('0.50') == 0.5);

Or use the parse() method of num, which creates an integer if possible and otherwise a double:

assert(num.parse('42') is int);
assert(num.parse('0x42') is int);
assert(num.parse('0.50') is double);

To specify the base of an integer, add a radix parameter:

assert(int.parse('42', radix: 16) == 66);

Use the toString() method (defined by Object) to convert an int or double to a string. To specify the number of digits to the right of the decimal, use toStringAsFixed() (defined by num). To specify the number of significant digits in the string, use toStringAsPrecision() (also in num):

// Convert an int to a string. assert(42.toString() == '42'); // Convert a double to a string. assert(123.456.toString() == '123.456'); // Specify ...

Get Dart: Up and Running now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.