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 Development, Coding Ninjas Studio Problems, Coding Ninjas Studio Interview Bundle, Coding Ninjas Studio Interview Experiences, Coding Ninjas Courses, Coding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog to help other ninjas grow. Happy Coding!