What Are The Packages In Flutter?

Topics Covered

What Are The Packages In Flutter?

Packages in Flutter are pre-built collections of code that provide more features and capabilities to your app. Flutter packages are designed to be reusable and modular, allowing developers to use existing code instead of writing it from scratch. This saves time and effort by providing pre-made solutions for typical problems. You can quickly integrate packages into your Flutter applications by including them as dependencies in the pubspec.yaml file.

Core Packages in Flutter

In Flutter, several core packages are considered fundamental to the framework. These packages are maintained by the Flutter team and are essential for building Flutter applications. Some of the core packages in Flutter include:

  • Flutter/material:
    This package provides the Material Design widgets and components, allowing developers to create visually appealing and responsive user interfaces.
  • Flutter/cupertino:
    This package provides the Cupertino widgets and components, which follow the iOS design guidelines. It enables developers to create iOS-styled interfaces.
  • Flutter/widgets:
    This package contains the foundation of Flutter's widget framework. It includes essential widgets for building user interfaces, handling gestures, layout, animation, and more.
  • Flutter/rendering:
    This package handles the low-level rendering aspects of Flutter. It provides classes and APIs for painting, layout, and compositing the visual elements of the user interface.

These core packages form the backbone of Flutter development, offering the necessary tools and components for building cross-platform mobile applications.

Third-Party Packages in Flutter

Flutter has a rich ecosystem of third-party packages that can be used to extend the functionality of your app. You can find these packages on the official Dart package repository. Some popular packages include:

  • Dio:
    Dio is a popular HTTP client library for Flutter and Dart. It provides a simple and elegant way to make HTTP requests, handle response data, and interact with RESTful APIs. Dio supports various features such as request cancellation, file uploads, request interception, authentication, and more.
  • Permission_handler:
    A package that provides a cross-platform (iOS, Android) API to request permissions and check their status. It allows you to request permissions during runtime and tell the OS which permissions your app might potentially use.
  • Flutter_svg:
    A third-party package that allows you to use SVG (Scalable Vector Graphics) images in a Flutter app. It provides a set of widgets that render SVG images, such as SvgPicture, which takes the path of the SVG file as an argument and renders the image.
  • Cloud_firestore:
    Flutter plugin for Cloud Firestore, a cloud-hosted, noSQL database with live synchronization and offline support on Android and iOS. The data is stored in documents, which are organized into collections. You can add, update, and delete data in your database using the plugin’s API.

Plugins in Flutter

In Flutter, plugins are packages that allow you to access platform-specific APIs using Dart code. Plugins are written in platform-specific code and can be used to access features such as camera, geolocation, and more.

You can find a wide variety of plugins on the official Dart package repository. Some popular plugins include permission_handler for requesting and checking permissions, geolocator for accessing geolocation data, and camera for controlling the camera.

You can also develop your plugins by writing platform-specific code and exposing it to Dart through a plugin package.

What is the Difference Between a Package and a Plugin?

PackagePlugin
Written purely in Dart.Combines Dart code with platform-specific code (Java/Kotlin for Android, Objective-C/Swift for iOS).
Generally used within the Flutter framework.Used to interact with platform-specific APIs or services.
Can be included as a dependency in pubspec.yaml.Can be included as a dependency in pubspec.yaml but may require additional setup and configuration.
Examples: http, shared_preferences, flutter_svgExamples: camera, firebase_auth, geolocator

Packages and plugins serve different purposes in Flutter. Packages provide reusable code and functionality within the Flutter framework, while plugins enable Flutter apps to access platform-specific APIs and services by combining Dart code with platform-specific code. Both packages and plugins contribute to the rich ecosystem of Flutter, providing developers with a wide range of options for enhancing their apps.

Using Packages

Searching for Packages

You can explore various resources to discover and find the packages that meet your specific needs. Here are some effective ways to search for packages:

  1. Pub.dev:
    Visit the official Flutter package repository at pub.dev. It is the primary source for finding Flutter packages. You can search for packages using keywords, explore different categories, view package details, and check their popularity and community support.
  2. Fluttergems.dev:
    fluttergems.dev is a curated list of Flutter packages and resources. It provides a categorized collection of popular and useful packages, making it easier to discover packages based on specific functionality or use cases.
  3. Flutter Community:
    The Flutter community is active on platforms like GitHub, Stack Overflow, and Reddit. You can join Flutter-specific communities and forums to ask for package recommendations or browse through discussions and recommendations shared by other developers.

Adding a Package Dependency to An App Using Flutter

To use any package in your Flutter application, you need to add it to your project's dependencies in the pubspec.yaml file.

Now run the flutter pub get command either in your terminal or using your editor to install the package in your app.

Removing a Package Dependency to an App Using Flutter

To remove a package dependency from an app using Flutter, you can use the flutter pub remove command. This command removes the specified package from the dependencies section of your pubspec.yaml file and updates your package dependencies.

Replace package_name with the name of the package you want to remove.

Conflict Resolution

Resolve the Conflict Manually
When conflicts occur between packages in Flutter, you can manually resolve them by specifying compatible versions of the conflicting packages in the pubspec.yaml file. This involves identifying the conflicting packages and their required versions and then selecting versions that are compatible with each other.

Use Dependency Overrides
In some cases, manual resolution may not be possible or practical. In such situations, you can utilize dependency_overrides as an alternative approach. In your pubspec.yaml file, under the dependency_overrides section, specifies the conflicting package and the desired version.

This approach should be used sparingly and as a last resort, as it can introduce its own set of issues if not managed carefully.

Developing New Packages

Step 1: Create the Package

Begin by creating a new Flutter project for your package.

This command will generate the necessary files and folders for a Flutter package, including a sample lib directory where you'll write your package code.

Now, write the necessary Dart code to implement the desired functionality of your package. Here we would create a package for a custom-styled button.

To use this widget we can write the following code:

Step 2: Implement the Package
Now, we can implement the created package in our project by giving its path location in the dependencies section of the pubspec.yaml file. If the package is present at the root of our project repository, we can use the following code:

Managing Package Dependencies and Versions

Package Versions

Each package in Flutter has a version number specified in its pubspec.yaml file. The current version and prior versions of a package can be viewed. Package versions are typically specified using semantic versioning, which follows the format major.minor.patch.

To prevent potential conflicts and breaking changes, it's recommended to specify a version range for packages. This can be achieved using the following formats:

  • Range Constraints:
    Define a minimum and maximum version for the package.
  • Caret Syntax:
    Similar to range constraints, caret syntax allows flexibility in accepting compatible package versions while maintaining stability.

By setting version range constraints, you ensure that your app remains functional even when packages are updated. This minimizes the risk of incompatibilities and helps maintain a reliable application.

Updating Package Dependencies

When you add a package to your Flutter project and run flutter pub get for the first time, Flutter saves the specific version of the package found in the pubspec.lock lockfile. This ensures that you and your team members will get the same version when running flutter pub get again.

If you want to upgrade to a newer version of the package to access new features, you can use the flutter pub upgrade command. This command retrieves the highest available version of the package that is allowed by the version constraint specified in the pubspec.yaml file.

Dependencies on Unpublished Packages

Sometimes, you may need to depend on packages that are not yet published on the Flutter package repository. In such cases, you have several options:

Path Dependency
A path dependency allows you to reference a local package that resides on your machine. This is useful when you are actively developing a package and want to test it within your project without publishing it to a public repository.

Now, you can import and use the classes and functionalities from the unpublished package in your Flutter project.

Git Dependency
Specify a Git dependency to fetch a package directly from a Git repository. This is useful when relying on packages hosted on platforms like GitHub.If your package is located at the root of the repository, you can use the following code:

Git Dependency using SSH
If your Git repository requires SSH authentication, you can configure your dependency to use SSH instead of the HTTPS protocol.

Git Dependency on a Package in a Folder
If your package resides in a specific folder within a Git repository, you can specify the path to that folder as a dependency.

By understanding and effectively managing package dependencies and versions, you can ensure the stability, compatibility, and functionality of your Flutter projects. Regularly updating dependencies and following best practices will help you leverage the latest features and improvements from the Flutter community while maintaining a reliable codebase.

Example App

As we have gone through flutter packages in detail. Now it's time to create an app that uses a Flutter package to perform some action. Here we would be using the url_launcher package to launch a website URL when we click on the specified button.

Step 1:
Add url_launcher as a dependency in your pubspec.yaml file:

Step 2:
Import the necessary packages in your Dart file:

Step 3:
Create required Widgets

example url launcher

widgets example

Conclusion

  • Packages are collections of pre-built code that add features and capabilities to your app, saving time and effort compared to starting from scratch.
  • Core packages are fundamental to the Flutter framework and include flutter/material, flutter/cupertino, flutter/widgets, and flutter/rendering.
  • Third-party packages extend the functionality of your app and can be found on platforms like pub.dev. Examples include permission_handler, geolocator, flutter_svg, and cloud_firestore.
  • Conflict resolution between packages can be done manually by specifying compatible versions in the pubspec.yaml file or using dependency overrides as a last resort.
  • Package versions follow semantic versioning, where major versions indicate potential breaking changes, minor versions introduce new features while maintaining compatibility, and patch versions address bugs and minor improvements.