Libraries

Of more interest is the import statement, which allows us to import classes for use in our code. From the start, Dart supports this feature that comes standard in all server-side languages. Better still, it works transparently in the browser.

As with the part statement, the source file that is being imported requires the library statement at the top of the file. In both cases, the library statement introduces an isolated scope for the code defined within. Consider, for example, a pretty-printing stopwatch class that might be used to time code.

libraries/pretty_stop_watch.dart
 
library​ pretty_stop_watch;
 
 
class​ PrettyStopwatch {
 
Stopwatch timer = ​new​ Stopwatch();
 
 
PrettyStopwatch.running() {
 
timer = ​new​ Stopwatch()..start(); ...

Get Dart 1 for Everyone now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.