Dart Basic- Libraries

What is a programming library?


A programming library is a collection of prewritten code that programmers can use to optimize tasks. Dart has a set of built-in libraries that are useful to store routines that are frequently used. A Dart library comprises of a set of classes, constants, functions, typedefs, properties, and exceptions.

Importing a Library


Importing a library means the library is available to the calling code. Multiple import statements are continued in a dart file.

The syntax for importing a library in Dart is given below −

import 'URI'

Some commonly used libraries are given below −

NoLibrary & Description
1dart:io
File, socket, HTTP, and other I/O support for server applications. This library does not work in browser-based applications. This library is imported by default.
2dart:core
Built-in types, collections, and other core functionality for every Dart program. This library is automatically imported.
3dart: math
Mathematical constants and functions, plus a random number generator.
4dart: convert
Encoders and decoders for converting between different data representations, including JSON and UTF-8.
5dart: typed_data
Lists that efficiently handle fixed-sized data (for example, unsigned 8-byte integers).

Example : Importing and using a Library

import 'dart:math'; 
void main() { 
   print("Square root of 25 is: ${sqrt(25)}"); 
}

Output

Square root of 36 is: 5.0

Example:

import 'dart:io';
import 'dart:math';
import 'dart:convert';
import 'dart:typed_data';

void main() {
  // Read a file and print its contents
  final file = File('example.txt');
  final contents = file.readAsStringSync();
  print(contents);
  
  // Generate a random number between 0 and 99
  final random = Random();
  final number = random.nextInt(100);
  print('Random number: $number');
  
  // Convert a map to JSON and back
  final map = {'name': 'John', 'age': 30};
  final json = jsonEncode(map);
  print(json);
  final decoded = jsonDecode(json);
  print(decoded);
  
  // Create a typed list of integers and sum its values
  final list = Int32List.fromList([1, 2, 3]);
  final sum = list.fold(0, (a, b) => a + b);
  print('Sum: $sum');
}

In this example, we use the dart:io library to read a file from the filesystem and print its contents. We also use the dart:math library to generate a random number between 0 and 99.

We use the dart:convert library to convert a map to JSON format and back again. This is a common task when working with web APIs that send and receive JSON data.

Finally, we use the dart:typed_data library to create a typed list of integers and sum its values. The typed list is more memory-efficient than a regular list because it can store fixed-size values without any overhead.

Encapsulation in Libraries


Encapsulation in Dart happens at the library level instead of the class level, unlike other object-oriented programming languages. Dart provides the facility to encapsulate or restrict access to 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.

Creating Custom Libraries


Dart also allows you to use your own code as a library. An example of a Custom Library is given below:

library calculator_lib;  
import 'dart:math'; 

//import statement after the libaray statement  
int add(int firstNumber,int secondNumber){ 
   print("inside add method of Calculator Library ") ; 
   return firstNumber+secondNumber; 
}  
int modulus(int firstNumber,int secondNumber){ 
   print("inside modulus method of Calculator Library ") ; 
   return firstNumber%secondNumber; 
}  
int random(int no){ 
   return new Random().nextInt(no); 
}

Next, we will import the library −

import 'calculator.dart';  
void main() {
   var num1 = 10; 
   var num2 = 20; 
   var sum = add(num1,num2); 
   var mod = modulus(num1,num2); 
   var r = random(10);  
   
   print("$num1 + $num2 = $sum"); 
   print("$num1 % $num2= $mod"); 
   print("random no $r"); 
} 

The output of the program will be-

inside add method of Calculator Library  
inside modulus method of Calculator Library  
10 + 20 = 30 
10 % 20= 10 
random no 0 

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.