Table of contents
1.
Introduction
2.
Importing a Library
3.
Custom Libraries
3.1.
1. Declaration
3.2.
2. Connection
3.3.
Example
4.
Frequently Asked Questions
4.1.
How can you import a Built-in Dart library?
4.2.
What is the use of the show keyword?
4.3.
What is the difference between the show and hide keyword?
5.
Conclusion 
Last Updated: Mar 27, 2024

Dart Libraries

Author Pradeep Kumar
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will look into the Dart Libraries. A library is a collection of routines in a programming language. Dart offers a set of built-in libraries that can be used to store commonly used routines.

If you are new to Dart and are yet to set up your environment for the language, you can check out this article.

Importing a Library

To use libraries in Dart, we must first import them. Importing a library makes its components available in the current file. For importing libraries, Dart includes a unique keyword import.

Syntax:

import 'libraryURI';

For example,

import 'dart:math';

// Function to calculate log base 2 of a number
double calculateLog(int a) {
  // log function is provided by the math library
  var ans = log(a);
  // the return type of log function is of double data type
  return ans; // return the ans
}

// main function to run the program
void main() {
  // Calling calculateLog function to calculate the log base 2 of the number 10.
  double res = calculateLog(10);
  // printing the output
  print(res);
}


Output:

2.302585092994046

We've imported the Dart Language's math library in the example above. The log function provided by the math library has been used in this program.

Custom Libraries

Dart provides users with the functionality of creating their own custom libraries. Custom libraries are the libraries that the user creates according to his need. Such libraries are known as user-defined libraries. 

Following are the two steps needed to create a custom Library:

1. Declaration

To declare a library in Dart, you must use the library keyword.

Syntax:

library 'customLibraryName';

2. Connection

To import a custom Library, use the import keyword. It can be imported from the same directory or a different one.

Syntax:

// Importing from the same directory
import 'customLibraryName';

// Importing from a different directory
import 'directoryName/customLibraryName';

Example

In this example, we will build a custom Library and import the same into another file.

customLibrary.dart

library customMathLibrary;

import 'dart:math';

// Function to calculate log base 2 of a number
double calculateLog(int a) {
  print("calculateLog function Invoked");
  // log function is provided by the math library
  var ans = log(a);
  // the return type of log function is of double data type
  return ans; // return the ans
}

// Function to calculate log base 10 of a number
double calculateLogBase10(int a) {
  print("calculateLogBase10 function Invoked");

  // log and ln function is provided by the math library
  var ans = log(a) / ln10;
  // the return type of log function is of double data type
  return ans; // return the ans
}

// This functions calculates e raise to power x
double calculateExponentOfE(int x) {
  print("calculateExponentOfE function Invoked");
  // Using the exp function provided by the math library
  var ans = exp(x);
  // return the answer
  return ans;
}

// This function calculates the max of two numbers
int calculateMaxOfTwoNumbers(int a, int b) {
  print("calculateMaxOfTwoNumbers function Invoked");
  // max function is provided by the math library
  var ans = max(a, b);
  return ans; // return the answer
}

// This function calculates the min of two numbers
int calculateMinOfTwoNumbers(int a, int b) {
  print("calculateMinOfTwoNumbers function Invoked");
  // min function is provided by the math library
  var ans = min(a, b);
  return ans; // return the answer
}


main.dart

import 'dart:math';

// Importing the customLibrary
import 'customLibrary.dart';

// main function to run the program
void main() {
  var num1 = 10;
  var num2 = 5;

  // Calling calculateLog function to calculate the log base 2 of the number 10.
  double res = calculateLog(10);
  print("The log base 2 of the number 10: " + (res).toString());

  // Calling calculateLog function to calculate the log base 10 of the number 10.
  double res2 = calculateLogBase10(10);
  print("The log base 10 of the number 10: " + (res2).toString());

  // Calling the calculateExponentOfE function to compute e raise to power 5
  double res3 = calculateExponentOfE(num2);
  print("e raise to power $num2: " + res3.toString());
 
  // Calling the calculateMaxOfTwoNumbers function to find out the max of num1 and num2
  int res4 = calculateMaxOfTwoNumbers(num1,num2);
  print("max($num1, $num2): " + res4.toString());
 
  // Calling the calculateMinOfTwoNumbers function to find out the max of num1 and num2
  int res5 = calculateMinOfTwoNumbers(num1,num2);
  print("min($num1, $num2): " + res5.toString());

}


Output:

calculateLog function Invoked
The log base 2 of the number 10: 2.302585092994046
calculateLogBase10 function Invoked
The log base 10 of the number 10: 1.0
calculateExponentOfE function Invoked
e raise to power 5: 148.4131591025766
calculateMaxOfTwoNumbers function Invoked
max(10, 5): 10
calculateMinOfTwoNumbers function Invoked
min(10, 5): 5

Frequently Asked Questions

How can you import a Built-in Dart library?

To import a Built-in Dart Library, we concatenate the keyword "dart:" before the library name. For example, To import the math library, use import "dart:math".

 

What is the use of the show keyword?

Show keyword is used to selectively import some functionality of the library instead of importing the whole library.

 

What is the difference between the show and hide keyword?

Show keyword selectively imports only the functionality followed by the keyword, while the hide keyword imports everything except the functionality mentioned after it.

Conclusion 

In this article, we have extensively discussed the Libraries in Dart. We hope that this blog has helped you enhance your knowledge regarding the Dart Libraries, and to learn more, check out our other article on Dart Operators.

And also, check out the awesome content on the Coding Ninjas Website,
Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass