DEV Community

Cover image for How To Install a Flutter Package
Stephen Michael
Stephen Michael

Posted on • Originally published at axxellanceblog.com

How To Install a Flutter Package

Introduction

In this short article, I will show you how you can easily install any Flutter package of your choice on your Flutter project to make development easier for you.

The original source of this article can be found here.

Difference Between a Package and a Plugin

A plugin is a type of package—the full designation is plugin package, which is generally shortened to the plugin.

What Are Packages

A pubspec file must be present in a directory for it to be considered a Dart package. A package can also include Dart libraries, apps, resources, tests, pictures, and examples in addition to the dependencies (which are stated in the pubspec). You can use a variety of packages on the pub.dev website that has been created by Google engineers and kind Flutter and Dart users.

What Are Plugins

A particular kind of package called a plugin package gives the app access to platform features. Web, macOS, Windows, Linux, Android (using Kotlin or Java), iOS (using Swift or Objective-C), and Linux plugin packages can all be created. For instance, a plugin might enable camera functionality for Flutter apps.

Including a Package dependency In Your Flutter Project

I will list out 5 steps you can take to achieve this, and I will use the HTTP package as an example in this guide, so here they are:

  1. Add http: ^package_latest_version to the dependencies section of the pubspec.yaml file inside the project folder.
  2. Installing the package using either your terminal, vs-code, or Android Studio/IntelliJ
    • From the terminal: Run flutter pub get.
    • From Android Studio/IntelliJ: Click Packages get in the action ribbon at the top of pubspec.yaml.
    • From VS Code: Click Get Packages located on the right side of the action ribbon at the top of pubspec.yaml.
  3. After installing it, you can import it on your project using the import statement in your dart code.
  4. If the package contains platform-specific code (e.g., Swift or Objective-C for iOS, Kotlin or Java for Android), your app must incorporate it. A complete restart of the application might be necessary to prevent problems like MissingPluginException while using the package because hot reload and hot restart only update the Dart code.

Here are a few pictures to visualize what I have said so far:

Code

Windows Terminal

Code

Conclusion

If you need more understanding on this I suggest you check the official flutter documentation, you can find the link elaborating more on this topic on the flutter doc website by clicking here.

Top comments (0)