Trusted answers to developer questions

Understanding and using Flutter packages

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

widget

Hey there!!!🙋🏿‍♀️🙋🏿‍♀️ I hope that you’re enjoying learning Flutter. It may seem difficult and challenging at first, but it will make sense with time. Read along to understand packages and how to use them in Flutter!

Packages, according to Flutterdocs, can be explained as “shared packages contributed by other developers to the Flutter and Dart ecosystems. This allows developers to quickly build an app without having to develop everything from scratch.”

Angela Yu defines packages as ‘open source libraries of code that other people have created, which you can incorporate into your project to make work easy and less time-consuming.’

That’s right, the English can be confusing, trust me, I think so too. 🤦‍♀️ Now, let’s break it down for us to understand.

Imagine an engineer who is great at what he does and has lots of clients waiting for him to build for them. Then, he gets this client that wants a house done in 3 months (Disclaimer!!!, I’m not an architect so I’m not actually sure how long it takes to build a mansion.) 😁😁😁😁

He starts building the house, but instead of molding his own bricks and drilling his own sand at the ocean, he decides to hire a brick company to supply all of that. This allows him to pay more attention to the parts that need it.

Now, for a Flutter developer, imagine you need to build an app but have minimal time and few advanced features. Instead of spending time on building these features from scratch, you could easily visit pub.dartlang.org. This page provides a stream of packages for you to pick from to solve a specific problem, thus saving you some time.

How to add Flutter packages to your project

STEP 1: Go to pub.dartlang.org and search for the package you need by either typing in a related keyword or the name of the package (if you already have one in mind).

Let’s use an example to explain this process: I will assume that you’re a badass flutter developer who has an e-commerce app that you have to develop in a short amount of time. This app requires a feature that allows people to make payments using Stripe.

All you need to do here is visit pub.dartlang.org and type in Stripe. A long list of packages will pop up (about 96 or so) that you can choose from. The high scores are shown on the right in a blue/red circular button, so you can easily pick out one that has the highest rating and fits your needs.

Flutter packages homepage
Flutter packages homepage

I will be experimenting with Stripe_fl designed by Ogbonda Chiziaruhoma (Chizih).

stripe_fl package
stripe_fl package

STEP 2: Clicking on ‘installing’ shows you how to use the package. Firstly, we depend on it by adding just two lines to our pubspec.yaml file:

dependencies: stripe_fl: ^0.1.0

Note: You got to be mindful of indentation in pubspec.yaml.

Secondly, you have to install the package by either clicking on the ‘Get Packages’ icon, located at the top of the pubspec.yaml page for VS Code users, or it’s equivalent in Android Studio or IntelliJ. For those of you who love to use the command line/terminal, you can just type pub get or Flutter pub get into your terminal. If you got the indentation, and everything went well, you won’t have any errors.

Finally, you have to import the package into your 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:stripe_fl/stripe_fl.dart’; And voila!!! Look who’s an awesome Flutter dev who knows what packages are and how to use them… Yeah, you!😉 Now, if you aren’t sure of how to use it in your own program, you can check the example tab that’s right beside the ‘installing’ tab on the package page.

If you encounter any challenges using a package or have suggestions on how to make a package work better, you could reach out to the author(s) via their email address. Email addresses of authors can be found on the package’s page as well. If you’re confident, go ahead and write your own package; I’ll be right here to cheer you on.

I really do hope this article helped you figure out what packages are, how to use them, and why it is important to consider using them as a developer.

If, by any chance, you need to ask more questions, do reach out to me on Twitter, I am always willing to answer questions and take criticisms.😊

Thank you for reading.

RELATED TAGS

dart
flutter
packages
stripe
Did you find this helpful?