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:
- Add http: ^package_latest_version to the dependencies section of the pubspec.yaml file inside the project folder.
- 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.
- From the terminal: Run
- After installing it, you can import it on your project using the import statement in your dart code.
- 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:
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.
Hey there! If you're interested in tech or programming articles, you should definitely give me a follow on any of the social media platforms below. I regularly post articles related to tech and programming, and I share valuable resources and insights. Plus, by following me, you'll be able to stay up to date on all the latest and greatest in the world of tech and programming. So, what do you say? Will you give me a follow?