Dart Library

Summary: in this tutorial, you’ll learn about the Dart library including creating a library, using a library, and splitting a library into parts.

Creating a library

In Dart, a library is a .dart file that has a library library_name declaration at the top of the file:

library library_name;Code language: Dart (dart)

By convention, the name of the library is in lowercase. If the name has multiple words, you should use underscores to separate words.

Also, the library name doesn’t need to be the same as the filename. For example, the following creates a library called shape placed in the shapes.dart file:

library shape;Code language: Dart (dart)

A library may use other libraries. Dart provides you with many built-in libraries. To use a library, you use the import statement.

For example, the following shape library uses the dart:math library:

library shape;

import 'dart:math';Code language: Dart (dart)

The following shows the complete shape library:

library shape;

import 'dart:math';

abstract class Shape {
  double get area;
}

class Circle extends Shape {
  double _radius;
  Circle({required double radius}) : _radius = radius;
  double get area => pi * pow(_radius, 2);
}

class Square extends Shape {
  double _length;
  Square({required double length}) : _length = length;
  double get area => pi * pow(_length, 2);
}Code language: Dart (dart)

The shape library has three classes. The Shape is an abstract class and Circle and Square classes are subclasses.

The Circle and Square classes have the area getter that returns the area of the corresponding shape. The area() methods use the pi and pow() function from the math library.

Using a library

To use a library in another library, you use the import statement. For example, suppose that you store the shapes.dart library in the libs folder:

├── libs
|  └── shapes.dart
├── main.dartCode language: Dart (dart)

The following main.dart uses the import statement to import the shapes.dart library:

import 'libs/shapes.dart';Code language: Dart (dart)

Once imported, you can use the classes of the shape library in the main.dart library:

import 'libs/shapes.dart';

void main() {
  var shapes = <Shape>{
    Circle(radius: 100),
    Square(length: 100),
  };

  double totalArea = 0;
  for (var shape in shapes) {
    totalArea += shape.area;
  }
  print('Total area: $totalArea');
}Code language: Dart (dart)

Output:

Total area: 41415.92653589793Code language: Dart (dart)

Splitting library

If a library is big, you can split it into multiple files. For example, you can split the shape library into the following files:

  • shape.dart – contains the Shape class
  • circle.dart – contains the Circle class
  • square.dart – contains the Square class

The new folder structure will be as follows:

library shape;

import 'dart:math';

part 'shape.dart';
part 'circle.dart';
part 'square.dart';Code language: Dart (dart)

First, create the shape.dart file:

part of shape;

abstract class Shape {
  double get area;
}Code language: Dart (dart)

In the shape.dart file, use the statement part of to indicate that the current file is a part of the Shape class.

Second, define the circle.dart file which is also a part of shape library:

part of shape;

class Circle extends Shape {
  double _radius;
  Circle({required double radius}) : _radius = radius;
  double get area => pi * pow(_radius, 2);
}Code language: Dart (dart)

Third, define the square.dart which is also a part of the shape library:

part of shape;

class Square extends Shape {
  double _length;
  Square({required double length}) : _length = length;
  double get area => pow(_length, 2) as double;
}Code language: Dart (dart)

Fourth, define the shapes.dart library:

library shape;

import 'dart:math';

part 'shape.dart';
part 'circle.dart';
part 'square.dart';Code language: Dart (dart)

In the shapes.dart file, include the shape.dart, circle.dart, and square.dart as the part of the shape library.

Summary

  • A library is a .dart file that includes the code.
  • Use the import statment import a library into another library.
  • Use the part of and part statements to split a library into multiple files.
Was this tutorial helpful ?