emjaycodes
Posted on July 23, 2022
Have you ever wanted to implement a certain functionality in your flutter code but got stuck because you didn’t know how to go about it? Well this is where flutter packages come in as they help to facilitate the building of your applications.
Packages contributed by other developers to the Flutter and Dart ecosystem are referred to as Flutter packages. This allows quickly building an app without having to develop everything from scratch according to the flutter docs. In other words, they are just open source libraries of code that other people have created which you can import into your project with ease.
Using Flutter Packages in Your Project
There are a few steps you need to take to be able to use these packages:
STEP 1:
You need to know the type of package you want to use in your project either by the specific name or by similar keywords. Then go to https://pub.dev/packages where you’ll see varieties of packages for different problems.
We score packages from 1 to 100 so we can choose the most efficient one. While there may be many packages, you should look for those with the best ratings.
For our example we’ll be using english_words by filiph.net.
A few terms to familiarize yourself with when browsing these packages:
*LIKES: * Likes offer a measure of how many developers have liked a package
PUB POINTS: A new way to measure quality. This includes several dimensions of quality such as code style, platform support, and maintainability.
Popularity: This provides insight into what other developers are using by determining the number of developers who use a package.
STEP 2:
Clicking on the ‘installing’ tab on the page shows you how to use the package. Firstly, we depend on it by adding just two lines to our pubspec.yaml file:
dependencies: english_words: ^4.0.0
Note: We have to be really careful about the Indentation in the pubspec.yaml file so that we don’t end up messing it up.
Secondly, we can also add it to our project by running a command on the terminal.
flutter pub add english_words.
And this will automatically add the package to the pubspec.yaml file. You’ll be notified that the pubspec.yaml file has been edited and you need to run a flutter pub get command.
STEP 3:
Now in our code, we can import the package into our Main.dart file by adding it to the top of the file (the desired file) you want the package to be called in.
import 'package:english_words/english_words.dart';
Let’s take a little example
import 'package:english_words/english_words.dart';
main() {
nouns.take(50).forEach(print);
}
The code above will print the top 50 most used nouns in the English language.
Conclusion
It's done! You have successfully added a flutter package into your project.
If you have any questions or concerns, feel free to contact the package developers through their email addresses listed on the package pages.
You can also help the flutterdev community by creating your own package if one doesn't exist yet.
Thanks.
Posted on July 23, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.