Skip to content
Related Articles

Related Articles

Dart – Libraries

View Discussion
Improve Article
Save Article
  • 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'

Dart




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


Output:

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’  

Dart




// 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:

Dart




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");  
       
}


Output:

Add Method
Multiplication Method
Subtraction Method

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

Encapsulation

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:

Dart




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


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

Dart




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


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:

Dart




// 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 

Dart




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


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

Dart




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


Output:

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

Below are the core libraries of Dart:

TYPE LIBRARY DESCRIPTION
Multi-Platform Libraries dart:async This library supports asynchronous programming, with classes such as  Future and Stream.
dart:collection This library contains classes and utilities that supplement the collection support in dart:core.
dart:convert This library contains encoders and decoders for converting between different data representations, including JSON and UTF-8.
dart:core This library contains built-in types, collections, and other core functionality for every Dart program.
dart:developer This library provides interaction with developer tools such as the debugger and inspector.
dart:math This library contains mathematical constants and functions, plus a random number generator.
dart:typed_data This library contains lists that efficiently handle fixed sized data files(for example, unsigned 8-byte integers) and SIMD numeric types.
Native platform libraries dart:io This library contains file, sockets, HTTP, and other I/O support for non-web applications.
dart:isolate This library supports Concurrent programming using isolates independent workers similar to threads.  
Web platform libraries dart:html This library contains HTML elements and other resources for web-based applications.
dart:indexed_db This library supports a Client-side key-value store with support for indexes.
dart:web_audio This library supports High-fidelity audio programming in the browser.
dart:web_gl This library supports 3D programming in the browser.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!