advertisement
Packages and their usage in Dart Programming Language
What is a Dart Package?
A package in Dart is a set of organized files that can be used to form a software library or a tool. To develop applications, built-in as well as third-party libraries or packages can be utilized. Dart has a set of default packages such as math, HTML, and collections which are loaded automatically at the start of the Dart console. Dart also facilitates the creation of user-defined packages.
Necessity for the Dart Package
A Dart package contains a single or a set of functions that help us resolve issues or add new features to applications. It also helps solve problems without having to write code from scratch. For example, let's say we are building an application requiring a videoconferencing feature. Implementing the package or writing the code ourselves might be a tedious and time-consuming task. In such a case, there is a possibility that a package may already exist which can handle the video conferencing feature. Hence, this package can be easily integrated into our application making the job much easier.
Exploring Usage
A package is utilized in applications that require libraries from the core library or third-party libraries. These packages generally contain reusable codes that can be used for several applications across all platforms. Dart package dependencies can be sent and pulled from the pub.dev Website and repository using the pub command line utility.
If packages other than the default packages are required, those packages must first be installed and explicitly loaded for use. Once a package is loaded, it can be utilized in the entire Dart environment. The Dart IDE provides built-in support for creating, updating, and publishing packages. Another way to acquire the same result is to utilize the pub.
Some of the important pub commands.
- Pub get: Utilized to get all the packages that the application depends on.
- Pub upgrade: Utilized to upgrade all the packages that the application depends on.
- Pub help: Utilized to get all the available pub commands. This can guide the user to work better with commands.
Types of Dart Packages
Functionality determines the categorization of Dart packages, such as:
- General-Purpose Packages
- Dart Core Library Packages
- Specialized Packages
A. General-Purpose Packages: These are the most commonly used packages that are used in almost all projects such as handling JSON objects, making API calls, and so on.
Some of the general-purpose packages.
- Characters: Utilized for manipulation of strings.
- HTTP: Utilized for the consumption of HTTP resources.
- Intl: Utilized for internationalization and localization.
- JSON_Serializable: This makes it easier to utilize the code generation package.
- Path: Utilized to manipulate path types.
- Stack_trace: parses and manipulates stack traces produced by the Dart implementation.
B. Dart Core Library Packages: These packages are built based on the Dart core library, thereby helping to add missing features and functionalities.
The Core library is a built-in, automatically imported library that provides basic collections such as List, Set, Maps, DateTime, and even errors. Dart has an extensive collection of core libraries that furnish the necessary prerequisites for day-to-day programming tasks Dart core libraries work on multiple platforms such as Dart default platforms, Dart native platforms, and Dart Web platforms.
Packages that expand on the core Dart libraries.
- Collection: This package expands on the dart: collection and adds more functions and classes to make collections easier.
- Io: This package expands on dart: IO and is used to work with files, sockets, and other 1/0 support for server apps.
- async: This package expands on dart: async and adds utility classes to make asynchronous computations easier.
C. Specialized Packages: Specialized packages are packages for mobile (flutter) and web development.
1. Plugin Packages
Plugin packages are specific packages that have an API and are written using Dart code along with one or more implementations that are platform-specific. It is possible to write plugin packages for operating systems such as iOS, Android, MacOS, Windows, or Linux as well as for developing Web applications.
There are quite a lot of packages for Android and iOS that are optimized for data as well as battery. Even though it is possible to write packages using Dart for already existing ones, there is no generic way for those being mobile-friendly. This results in spending a significant amount of resources on this issue and hence, it is better to avoid writing all packages completely using Dart only.
It is important to note that some libraries such as those for crash reporting are part of the OS. An example of this is Google Mobile Services (GMS). GMS is a collection of Google apps and APls that improve the User Experience by providing support functionality across devices. These libraries minimize data use. Aggregate data from multiple applications and send them in batches to the applications requesting data. . Dart code is never a part of the OS and hence, does not adhere to this.
Creation of User-Defined Packages in Dart
Packages are utilized by Dart to share libraries and tools. Besides the built-in packages provided by Dart such as dart:io, dart:core, and dart:math, users can also create their packages. The directory and structure of a package are automatically created in Dart when the create command is utilized.
1 pubspec.yaml:
All Dart applications have a pubspec.yaml file and this file includes metadata. The package author, version, name of the application, and description are all stored as part of the metadata. The pubspec.yaml is also utilized for downloading libraries that are required by the application when programming.
2 .gitignore:
This command tells Git which files it has to ignore if and when the project is committed to the GitHub repository.
3. README.md:
The README file gives more details about the package. If this package is published to pub.dev, the contents of the README file will appear on the landing page of the package.
4. CHANGELOG.md:
The CHNAGELOG.md file gives information on the latest updates for the project or the application. This file keeps track of all the changes made to the project organized by date and version. So, any notable improvements or bugs can be identified using the CHANGELOG.md file.
5. Example of a Simple Application Using a User-defined package
In the mypackag package, implement an Arithmetic class that takes two integers and performs arithmetic operations. This class can be created inside lib/src/mypackage_base.dart.
advertisement
Conversation
Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!
Join the discussion and share your insights now!
Comments 0