Skip to content
Related Articles

Related Articles

Improve Article
Dart – Libraries
  • Last Updated : 22 Mar, 2021

Dart is the open-source programming language originally developed by Google. It is meant for both the server-side and the user side. The Dart SDK comes with its compiler – the Dart VM and a utility dart2js which is meant for generating JavaScript equivalent of a Dart Script so that it can be run on those sites also which don’t support Dart. 

Dart is an Object-oriented language and is quite similar to that of Java Programming. Dart is extensively used to create single-page websites and web-applications. The best example of a dart application is Gmail.

In this article, we will be looking into the dart libraries and the process of working with them.

Importing Libraries

For using libraries in the dart, we must import them first. Importing makes the components of a library available in the current file. Dart has a special keyword import for importing libraries.

Syntax: import 'dart:sync'


// Example showing importing a library
// Importing built-in library  
import 'dart:math';   
void main() {   
   print("Square root of 25 is: ${sqrt(25)}");   


Square root of 25 is: 5

When importing a library file from another package, “package: directive” is used  to specify the URI of that file:

import 'package:utilities/utilities.dart';

Creating Custom Libraries

Custom libraries are the libraries that the user creates according to his need. Such libraries are known as user-defined libraries. Dart supports the creation of such libraries, and we can import them when needed. This takes place in basically two steps:

1. Declaration

Library name must be declared using the keyword “library”

library ‘my_lib’  //any random lib name

2. Connection

A library can be connected within the same directory or from another directory.

//within same directory

import ‘library_name’  

//from another directory

import ‘dir/library_name’  


// Example illustrating creation of a custom library
library basic_calc;    
import 'dart:math';   
// library content  
int add(int a,int b){   
   print("Add Method ") ;   
   return a+b;   
int multiplication(int a,int b){   
   print("Multiplication Method") ;   
   return a*b;   
int subtraction(int a,int b){   
   print("Subtraction Method") ;   
   return a-b;   
int modulus(int a,int b){   
   print("Modulus Method") ;   
   return a%b;   

We have created a custom library named “basic_calc“. Now we have to import our lib in a current file:


import 'basic.dart';  
void main() {  
   var a = 50;   
   var b = 30;   
   var sum = add(n1,n2);   
   var mod = modulus(n1,n2);   
   var mul = multiplication(n1,n2);  
   var div = divide(n1,n2);  
   var sub = subtraction(n1,n2);  
   print("$n1 + $n2 = $sum");   
   print("$n1 %  $n2= $mod");   
   print("$n1 + $n2 = $mul");   
   print("$n1 - $n2 = $sub");  


Add Method
Multiplication Method
Subtraction Method

Modulus Method
50 + 30 = 80
50 % 30= 20
50 * 30 = 1500
50 - 30 = 20


Another feature provided by dart to its users is the encapsulation of libraries. Encapsulation is used for combining data and functions into a single unit called class. We can achieve encapsulation in dart by using the _(underscore), followed by the identifier. The _(underscore) symbol is used to make the library’s content completely private.

NOTE: Encapsulation in Dart takes place at library level instead of class-level, unlike other OOP languages.

Syntax: _identifier



// Example showing encapsulation in dart
// Define a library naming cake
library cake;
class MainCake{
// non-private property
// list of strings
 List<String> randomPieceOfCakes = ['chocolate',
 // private properties
 String _pieceOfCake1 = "chocolate";
 String pieceOfCake2 = "butterscotch";

Here we will import the cake library that we created above:


import 'cake.dart';
void main(){
  MainCake newCake = new MainCake();
  // non-private property -  randomPieceOfCakes
  // private property - piece of cake
  // private property error
  // non-private private - piece of cake

As Keyword

We can import multiple libraries within a single current file. But if we create two or more functions with the same name, the compiler would be unable to differentiate between the two and will provide the wrong output. To avoid this, dart provides us keyword “as” for naming the alias of a library.

Syntax: import 'my_lib' as prefix  



// Example showing the use of as keyword
// Defining a lib names greetings
library greetings;    
void sayHi(msg){   
   print("Hello coder. Welcome to ${msg}");  

Now we will define another library 


library hellogreetings;   
void sayHi(msg){   
   print("${msg} has solutions of all your problems");   

Now, we import the above libraries with them as the prefix


import 'greetings.dart';   
import 'hellogreetings.dart' as gret;    
// using as prefix avoids function name clashes   
void main(){   
   // To eliminate the name confliction


Hello coder. Welcome to GFG
GFG has solutions of all your problems

Below are the core libraries of Dart:

Multi-Platform Librariesdart:asyncThis library supports asynchronous programming, with classes such as  Future and Stream.
dart:collectionThis library contains classes and utilities that supplement the collection support in dart:core.
dart:convertThis library contains encoders and decoders for converting between different data representations, including JSON and UTF-8.
dart:coreThis library contains built-in types, collections, and other core functionality for every Dart program.
dart:developerThis library provides interaction with developer tools such as the debugger and inspector.
dart:mathThis library contains mathematical constants and functions, plus a random number generator.
dart:typed_dataThis library contains lists that efficiently handle fixed sized data files(for example, unsigned 8-byte integers) and SIMD numeric types.
Native platform librariesdart:ioThis library contains file, sockets, HTTP, and other I/O support for non-web applications.
dart:isolateThis library supports Concurrent programming using isolates independent workers similar to threads.  
Web platform librariesdart:htmlThis library contains HTML elements and other resources for web-based applications.
dart:indexed_dbThis library supports a Client-side key-value store with support for indexes.
dart:web_audioThis library supports High-fidelity audio programming in the browser.
dart:web_glThis library supports 3D programming in the browser.

My Personal Notes arrow_drop_up
Recommended Articles
Page :