Dart LibrariesIn Dart, the library is the collection of the routine or set of programming instructions. Dart consists of many sets of built-in libraries that are beneficial to hold routines (functions, set of classes, etc.), and regularly used. A Dart library contains constants, functions, properties, exceptions, and typedefs, and set of classes. Importing a libraryTo work with the library, we must import it into the current program. The Dart provides the import keyword, which is used to make the library available in the current file. We can use multiple libraries in a single file. For example - Dart built-in library URIs is used as dart scheme to refer to a library. Other libraries can use a file system path or the package: scheme to specify its URIs. The package manager pub in Dart provides the libraries and uses the package scheme. We are describing some commonly used libraries below.
Let's understand the following example of importing and using a library function. Example - Importing and using a LibraryOutput: Square root of 25 is: 5.0 Explanation:In the above code, we imported the built-in library 'dart:math'. It provides the many built-in mathematical function, here we used the sqrt() function with number. It takes a number as an argument that we want to find its square root of. We passed an integer number 25 in sqrt() function, and it retuned an output as 5. Encapsulation in LibrariesDart provides the facility to encapsulate or restrict access 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. The syntax is given below. Syntax:Example -We define a library called Greetings that has a private function. The above file saves as greetings.dart, now we import the library. Output: After running the above code, it throws an error because we have declared the library with the private method and try to access it in other file. Unhandled exception: No top-level method 'w._sayHi' declared. NoSuchMethodError: method not found: 'w._sayHi' Receiver: top-level Arguments: [...] #0 NoSuchMethodError._throwNew (dart:core-patch/errors_patch.dart:184) #1 main (file:///C:/Users/Administrator/WebstormProjects/untitled/Assertion.dart:6:3) #2 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:261) #3 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:148) Creating Custom Libraries (User-defined Library)We can also use our own code as a library and import it when needed. This type of library is called a custom library. Below are the steps to create a custom library. Step - 1: Declaring a Library The library statement is used to create a library explicitly. The syntax is given below. Syntax:Step - 2: Connecting a Library We can connect a library in two ways.
Let's understand the following example - Example - Custom LibraryNow we import the above custom file in current file called 'library.dart'. Output: inside add method of calculator_simple Library inside modulus method of calculator_simple Library inside multiplication method of calculator_simple Library inside subtraction method of calculator_simple Library 30 + 10 = 40 30 % 10= 0 30 + 10 = 300 30 - 10 = 20 Copy the above code and paste it into your dart editor and observe the result. Note - The custom library must be imported by its saved file name such as we imported it in the current working file with the calculator_simple name.Name Alias of LibraryDart allows us to import multiple libraries into the current working file, but if we create the same function name within the different libraries, it will create conflict while accessing these functions. The Dart compiler might be confused to identify the particular function in different library. To overcome this scenario, Dart provides the as keyword for specifying the prefix. The syntax is given below. Syntax:Let's understand the following example - Example -First, we define a library: greeting.dart Next, we define the new library: hellogreetings.dart Now, we import the above libraries with the as prefix. Output: Learn the Dart with JavaTpoint JavaTpoint provides the tutorial on all technical related topic Next TopicDart Generators |