Add Your Heading Text Here

What is Dart Library ?

A Library in any programming language is a set of code which was pre-written by any developer so that others can use them in their own programs just by importing them in program.

Libraries are collection of functions, variables, typedefs, constructors, classes and many more concepts to develop some useful code reusable to any developers just by adding some reference points which is library imports.

Libraries are used to reuse the code so that people do not have to write the set of codes which are frequently used.

For ex :

Print() method is function written in dart:core library so that for displaying a data we don’t need to write that code every time. You just have to import specific library whose functions or classes you want to use.

What is Library Import ?

Importing a library makes that particular set of code(which may contain different functions, classes, variable and typedef etc.) which we want to use in our current program accessible to the user.

We can import multiple library at a time in the program.

Every programming languages have therir own set of predefined libraries which helps users to save their time and efficiency.

Dart also have some libraries which makes our coding more easier.Some of them are :

dart:core

Built-in types, collections, and other core functionality. This library is automatically imported into every Dart program.

dart:async

Support for asynchronous programming, with classes such as Future and Stream.

dart:math

Mathematical constants and functions, plus a random number generator.

dart:convert

Encoders and decoders for converting between different data representations, including JSON and UTF-8.

dart:html

DOM and other APIs for browser-based apps.

dart:io

I/O for programs that can use the Dart VM, including Flutter apps, servers, and command-line scripts.

To import any library in the dart “import” keyword is used.

The only required argument to import any library is a URI(Uniform Resource Identifier- is the locator of the library resource in project) specifying the library.

For built-in libraries, URI syntax for importing library is as below :

// libraryName is the Name of the predefined dart library.
import ‘dart:libraryName’;

For custom libraries(which can be created by any random user), URI syntax for importing library is as below :

// where test/test.dart is URI of the custom library.
import ‘package:test/test.dart’;

For better experiencing the use of custom libraries create two dart files. Let say one is test.dart and other is PoliticiansTest.dart

create Test.dart file and copy below code in that file to test.

// so here to use PoliticiansTest class which is located inside the PoliticiansTest.dart file, we have to import that PoliticiansTest.dart library.
import 'package:DartSample/PoliticiansTest.dart';
void main(){
  PoliticiansTest politiciansTest=new PoliticiansTest();
  politiciansTest.politicianFromTheCountry("N. Modi", "India");
  politiciansTest.politicianFromTheCountry("B. Obama", "USA");
  politiciansTest.politicianFromTheCountry("N. Mandela", "Africa");
}
-Create PoliticiansTest.dart file and copy below code in that file to test. 
class PoliticiansTest{
  politicianFromTheCountry(String name,String country){
    print("$name is politician from the country $country");
  }
}

Output :

N. Modi is politician from the country India
B. Obama is politician from the country USA
N. Mandela is politician from the country Africa

Similarly, you can also provide prefix for libraries to simplify the library import or distniguish the component when there are two libraries having same method or class etc.

as keyword is used for providing prefix to the library imported.

Create Library1.dart file and copy below code in that file to test.

testLibrary(){
print(“Testing Library First”);
}

Create Library2.dart file and copy below code in that file to test.

testLibrary(){
print(“Testing Library Second”);
}

Create test.dart file and copy below code in that file to test.

// prefixing lib1.dart with lib2 using as keyword
import 'package:DartSample/Library1.dart' as lib1;
import 'package:DartSample/Library2.dart' as lib2;
void main(){
	lib1.testLibrary();
	lib2.testLibrary();
}

Output :

Testing Library First
Testing Library Second

By prefixing any library we can access class , variables and methods with prefix. In above example, we have added prefix to the library1.dart as lib1. Now we can access any class, variables and methods from the library1.dart using that prefix like testLibrary() method from the library.

Therefore Library is a great way for code reusability in any manner to improve the programming efficiency. Hence, results in greater productivity.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Show Buttons
Hide Buttons