Getting Started with Pub.dev and Flutter Lints

Have you tried creating your first Flutter app yet?

This is as simple as running flutter create billion_dollar_app on the terminal (or doing the same directly from VSCode or Android Studio).

After creating the project, you’ll notice that some files and folders have been generated:

‣ android/
‣ ios/
‣ lib/
  ‣ main.dart
‣ linux/
‣ macos/
‣ test/
‣ web/
‣ analysis_options.yaml
‣ pubspec.yaml
‣ README.md

The most important file is called pubspec.yaml. This is used to specify your application’s dependencies. These resources explain how this file works and how to use it to install packages:

Installing Packages from pub.dev

The Flutter SDK gives you everything you need to build apps using the Material or Cupertino widgets (we’ll talk about those in the upcoming lessons).

But apps are made of more than just UI.

And rather than reinventing the wheel, you’ll want to install packages from pub.dev, the official package repository for Dart and Flutter apps.

Installing packages is easy, and you can do it directly from the command line:

# Install http package as a dependency
dart pub add http
# Install build_runner package as a dev dependency
dart pub add build_runner --dev

Or, if you use VSCode, you can hit CMD+SHIFT+P to open the command palette and use the “Dart: Add Dependency” command:

Adding a dependency with the command palette in VSCode

Then, you can enter a comma-separated list of packages, and VSCode will suggest all the available matches:

Adding multiple packages with the command palette in VSCode

How to choose which packages to install?

When deciding if a package suits your needs, there are a few factors to consider:

Remember that when you install a package, you depend on it. And depending on packages that are buggynot maintained, and poorly documented won’t make you a happy bunny. So try to choose high-quality packages from reputable authors and organizations.

A great way to discover the best packages is to check out the Package of the Week playlist on YouTube.

Linting

When you create a Flutter project, the flutter_lints package will be installed by default.

Lints are rules that are used to encourage good coding practices, and they help you write better and safer code.

To learn how to make the most of Flutter lints, right in your IDE, watch this video:

flutter_lints may be the default linter package - and a good one when you’re just starting.

But as you become more experienced, consider using packages such as LintVery Good Analysis (VGA), or DCM (paid), which offer much stricter linting rules.

If you want to go deeper into this topic, read this guide:

If you really want to, you can even write your own lint rules using the custom_lint package.

Checklist for New Flutter Projects

Enabling linter rules is one of the best things you can do when you start a new Flutter project.

But if you want to build some serious apps with a backend, multiple environments, automated tests, and more, there are many things to consider. We’ll cover some of these things in the upcoming emails. But for now, here’s a helpful checklist:

Daily Challenge - Add Flutter Lints

If you have an old Flutter project lying around, try this:

  • Upgrade to the latest version of the flutter_lints package in your pubspec.yaml file (under dev_dependencies).
  • Try fixing all linter errors with the Quick Fix option in VSCode or by running dart fix on the terminal.

Happy coding!

Questions? Let's chat