Dart Libraries
In Dart, the library is the collection of the routine or set of programming instructions. Dart consists of many sets of built-in libraries that are beneficial to hold routines (functions, set of classes, etc.), and regularly used. A Dart library contains constants, functions, properties, exceptions, and typedefs, and set of classes.
Importing a library
Importing makes the components in a library available to the caller code.
The import keyword is used to achieve the same. A dart file can have multiple import statements.
Built in Dart library URIs use the dart: scheme to refer to a library.
Other libraries can use a file system path or the package: scheme to specify its URI.
Libraries provided by a package manager such as the pub tool uses the package: scheme.
The syntax for importing a library in Dart is given below
import 'URI'
Consider the following code snippet
import 'dart:io'
import 'package:lib1/libfile.dart'
If you want to use only part of a library, you can selectively import the library. The syntax for the same is given below
import 'package: lib1/lib1.dart' show foo, bar;
// Import only foo and bar.
import 'package: mylib/mylib.dart' hide foo;
// Import all names except foo
We are describing some commonly used libraries below.
Library | Description |
---|---|
dart:io | This library consists of File, HTTP, socket, and other I/O support for server applications. This library is not suitable for browser-based applications. We don’t need to import explicitly because it is imported by default. |
Dart:core | This library consists of Collection, built-in types, and other core functionality for each dart program. It is imported by default. |
Dart: math | This library consists of the rich mathematical functions, constant, and random number generator. |
Dart: convert | It is used to Encoders and decoders for converting the different data representations such as JSON and UTF |
Dart: typed_data | It represents the lists that store the fixed-sized data efficiently (for example - unsigned 8-byte integer). |
Importing and using a Library
Example
import 'dart:math'; // Importing built-in library
void main() {
print("Square root of 36 is: ${sqrt(36)}");
}
Output
Square root of 36 is: 6.0
Encapsulation
Dart provides the facility to encapsulate or restrict access the content of the dart library.
It can be done by using the
_(underscore)
, followed by the identifier.The
_(underscore)
symbol makes the library’s content completely private.
Syntax
_identifier
Creating Custom Libraries (User-defined Library)
We can also use our own code as a library and import it when needed. This type of library is called a custom library. Below are the steps to create a custom library.
Step 1 - Declaring a Library
The library statement is used to create a library explicitly. The syntax is given below.
Syntax
library library_name
// library contents go here
Step 2 - Connecting a Library
We can connect a library in two ways.
Within the same directory
import 'library_name'
From a different directory
import 'dir/library_name'
Library Prefix
If you import two libraries with conflicting identifiers, then you can specify a prefix for one or both libraries. Use the ‘as’ keyword for specifying the prefix.
Syntax
import 'library_uri' as prefix
Name Alias of Library
Dart allows us to import multiple libraries into the current working file, but if we create the same function name within the different libraries, it will create conflict while accessing these functions.
The Dart compiler might be confused to identify the particular function in different library.
To overcome this scenario, Dart provides the as keyword for specifying the prefix.
Syntax
import 'library_uri' as prefix