Skip to main content

Maintain great Dart Code Quality with these Simple Tips

Maintain great Dart Code Quality with these Simple Tips

Code quality is a crucial thing in development, no matter in what programming language you code. It’s especially important when working in a team or on an open source project, but also when you work on a personal project which could possibly expand with more and more features. That’s why I will show you how to configure advanced linting in Dart & Flutter. Let’s get right into it!

Happy reading!

The problem with Dart’s Linter

The linter of Dart is great, but there are some other problems, especially a while ago. For example, when creating a button, it’s not rare that the parameter onPressed is required. Back in the days, it was just a warning, which didn’t let the app stop working. But not specifying a required parameter will stop the app from working. So let’s change this to a real error!

https://i0.wp.com/resocoder.com/wp-content/uploads/2020/01/warning-missing-required-parameter.png?w=613&ssl=1

Changing linter options

Maybe you have seen the analysis_options.yaml a few times in your Flutter projects. This file is responsible for all the code linting in your project. Let’s see how we can change this:

As you can see, we set the linting rule missing_required_param from a default warning to an error. There are four possible values: ignore, info, warning, and error.

Now, we can see, that it gives us a full-blown error:

https://i0.wp.com/resocoder.com/wp-content/uploads/2020/01/error-missing-required-parameter.png?w=609&ssl=1

But there are so many more linter rules. You can view every linter option here.

Predefined linter rules

It’s great that we can customize everything, but that is way too time-consuming. Luckily, dart’s linter is currently way better than before and there are many packages that have great presets. I personally like the lint package, which you can find here.

Further reading & Conclusion

In this article, you have learned the basics of linting in Dart. You have seen how easy it is to use and how it can improve your code quality.

If you want to discover great packages that have great code quality under the hood, like Riverpod, Freezed or Isar, you can find tutorials here.

In the following few articles, I will introduce more somewhat complicated packages and explain them. If you don’t want to miss this, I recommend you to follow me. I tried my best to write the easiest tutorial which everyone understands. If you appreciate this work, I would be very grateful if you could support this quality content and give me some claps!

Thanks for reading, have a nice day!

Sources:

https://resocoder.com/2020/01/17/dart-linting-tutorial-healthy-flutter-codebase-with-analysis_options-yaml/#t-1660995442024

https://dart.dev/tools/linter-rules

https://dart-lang.github.io/linter/lints/

https://pub.dev/packages/lint