Sage-Code Laboratory
index<--

Dart Libraries

Dart is a modular language. It consist of core functionality and libraries. This is code written in Dart language that can be reused in multiple projects. Multiple libraries are gorupped into packages.

Library Members

Any dart file can be a library. Members of library are by default public, except the members who start with underscore. These members are private, and visible just inside the library.

You can create your own libraries and distribute these libraries as API for other programmers to use. In this case you extend the language with new functions. Making libraries require advanced skills that are not in our scope for this tutorial.

Using a library

You must use import keyword to specify how a namespace from one library is used in the scope of another library. You can import the entire library, a library from a specific package and you can also import only partially a library.

Example:

Maybe you remember but we have done this example before to study the switch statement. Take a look again and spot the "import" directive to understend how is used here.

//using "math" library
import 'dart:math';
  
void main() {
  var rng = new Random();
  for (int i = 0; i < 10; i++) {
    var v = rng.nextInt(5);  
    switch (v) {
      case 1:
          print("first v=$v");
          break;
      case 2:
          print("second v=$v");
          break;
      case 3:
      case 4:    
          print("more v=$v");
          break;
      default:
          print("default v=$v");
          break;
    }
  }
}

In previous example we have used Random() function that is not available in Dart core. Therefore we have imported this library in local namespace using import directive that is on very top of the program.

Read more: Using Libraries

THE END:

This was a free tutorial for beginers about Dart language. If you want to learn more follow Dart homepage. We will probably add more details to our tutorials but will never be equal with reference doc. Thanks for reading!


Go back to: Dart Tutorial