Dart is an Object-oriented language and is quite similar to that of Java Programming. Dart is extensively used to create single-page websites and web-applications. The best example of a dart application is Gmail.
In this article, we will be looking into the dart libraries and the process of working with them.
For using libraries in the dart, we must import them first. Importing makes the components of a library available in the current file. Dart has a special keyword import for importing libraries.
Syntax: import 'dart:sync'
Square root of 25 is: 5
When importing a library file from another package, “package: directive” is used to specify the URI of that file:
Creating Custom Libraries
Custom libraries are the libraries that the user creates according to his need. Such libraries are known as user-defined libraries. Dart supports the creation of such libraries, and we can import them when needed. This takes place in basically two steps:
Library name must be declared using the keyword “library”
library ‘my_lib’ //any random lib name
A library can be connected within the same directory or from another directory.
//within same directory
//from another directory
We have created a custom library named “basic_calc“. Now we have to import our lib in a current file:
Add Method Multiplication Method Subtraction Method Modulus Method 50 + 30 = 80 50 % 30= 20 50 * 30 = 1500 50 - 30 = 20
Another feature provided by dart to its users is the encapsulation of libraries. Encapsulation is used for combining data and functions into a single unit called class. We can achieve encapsulation in dart by using the _(underscore), followed by the identifier. The _(underscore) symbol is used to make the library’s content completely private.
NOTE: Encapsulation in Dart takes place at library level instead of class-level, unlike other OOP languages.
Here we will import the cake library that we created above:
We can import multiple libraries within a single current file. But if we create two or more functions with the same name, the compiler would be unable to differentiate between the two and will provide the wrong output. To avoid this, dart provides us keyword “as” for naming the alias of a library.
Syntax: import 'my_lib' as prefix
Now we will define another library
Now, we import the above libraries with them as the prefix
Hello coder. Welcome to GFG GFG has solutions of all your problems
Below are the core libraries of Dart:
TYPE LIBRARY DESCRIPTION Multi-Platform Libraries dart:async This library supports asynchronous programming, with classes such as Future and Stream. dart:collection This library contains classes and utilities that supplement the collection support in dart:core. dart:convert This library contains encoders and decoders for converting between different data representations, including JSON and UTF-8. dart:core This library contains built-in types, collections, and other core functionality for every Dart program. dart:developer This library provides interaction with developer tools such as the debugger and inspector. dart:math This library contains mathematical constants and functions, plus a random number generator. dart:typed_data This library contains lists that efficiently handle fixed sized data files(for example, unsigned 8-byte integers) and SIMD numeric types. Native platform libraries dart:io This library contains file, sockets, HTTP, and other I/O support for non-web applications. dart:isolate This library supports Concurrent programming using isolates independent workers similar to threads. Web platform libraries dart:html This library contains HTML elements and other resources for web-based applications. dart:indexed_db This library supports a Client-side key-value store with support for indexes. dart:web_audio This library supports High-fidelity audio programming in the browser. dart:web_gl This library supports 3D programming in the browser.