Flutter Static Analysis, Linting

Static analysis allows you to find problems before executing a single line of code. There are two major groups of static analysis, linting, and code-cycle analysis. In this article, I will cover using the dart analyzer to lint analyze flutter code.
Background
The problem is while Google does an excellent job at giving you an introduction to widgets; it’s not the techniques you really need to have mastered to develop a professional application and in a mature market. That is my self-assigned job in pushing out medium articles and my flutter design and development book series.
I am finding that by showing the cool visual and animation that I can get you guys and gals to do the boring devOPS and OOP and FP to get you to the point for flutter expert. Some subjects covered are:
DevOPS
Tuning A Cheap MS Windows Laptop for Flutter Development https://readmedium.com/tuning-a-cheap-ms-windows-laptop-for-flutter-app-development-572d09cd4d19
Log Driven Learning Flutter https://readmedium.com/log-driven-learning-flutter-d76b49b75a8c
UML Coolness in Flutter https://itnext.io/uml-coolness-in-flutter-6bb14217b5f2
Getting Real Code Coverage https://readmedium.com/getting-real-code-coverage-951231afa2bc
Lcov on Windows https://readmedium.com/lcov-on-windows-7c58dda07080
Test Secrets, Test Suites https://itnext.io/test-secrets-test-suites-99f8390b8d4b
How To, Flutter Internal Packages https://itnext.io/how-to-flutter-internal-packages-cad1285fe8c
An Architecture Layout https://readmedium.com/an-architecture-layout-8f414271b2b4
Logging, The Expert Way https://readmedium.com/logging-the-expert-way-5beb5c967e44
Catch Flutter Application Exceptions https://itnext.io/catch-flutter-application-exceptions-cad036d0fd4e
Flutter Perfect SetUp https://readmedium.com/flutter-perfect-setup-c5462b412f78
Cool Flutter Docs https://readmedium.com/cool-flutter-docs-383b951d7feb
Flutter Static Analysis, Dart Code Metrics https://readmedium.com/flutter-static-analysis-dart-code-metrics-c9ec484f4e0f
Visual
Animations Users Love(rive) https://readmedium.com/animations-users-love-75a57a8cad5
Full Flutter Background Trick https://readmedium.com/full-flutter-background-trick-d1ea813470d2
Cool Rive Background Animation in Flutter Apps https://readmedium.com/cool-rive-background-animation-in-flutter-apps-2fd5e58bc81b
Flutter Cross-Platform Tricks https://itnext.io/flutter-cross-platform-tricks-2196986c619c
Writing And Teaching
Medium Writing Secrets https://readmedium.com/medium-writing-secrets-a9b3d408b9e3
Since, my articles appear in multiple publications the best way to keep updated to those posting is to join one of these social platforms and follow me using my profile links:
LinkedIN https://www.linkedin.com/in/fredgrottstartupfluttermobileappdesigner/
Xing https://www.xing.com/profile/Fred_Grott/cv
Discord https://discordapp.com/users/9388/
Gitter(join this forum to get the article links) https://gitter.im/flutter/flutter
Slack https://app.slack.com/client/TGT6YF2J1/CGS4QDJ56/user_profile/UHK8PNRGU
Twitter https://twitter.com/fredgrott
Medium https://fredgrott.medium.com
Reddit FlutterDev Subedit(just join this forum and you will get the notices via reddit) https://www.reddit.com/r/FlutterDev/
As always, the code to both the articles and the books can be found at:
flutter design and arch rosetta at Github https://github.com/fredgrott/flutter_design_and_arch_rosetta
And, the plugins I contribute to are
Flutter Platform Widgets https://github.com/stryder-dev/flutter_platform_widgets
Flutter Rive(player) Plugin https://github.com/rive-app/rive-flutter
Dart Analyzer
The computer language that Flutter uses, Dart, has a tool called the analyzer:
https://pub.dev/packages/analyzer
Your dart sdk is in this location in your flutter sdk:
fluttrsdk/bin/cache/dark-sdk
And the analyzer tool is in the bin sub-folder. Many tools use it, for example, the pub.dev server uses the dartfm tool to run this analyzer on all plugins submitted to that site. And, obviously, since it can be configured with an analysis_options.yaml file the IDEs can also execute the dart analyzer tool.
Why Choose A Lint Rules Group
Why do we need to choose a lint rules group to use in our flutter projects? Let me show you why, just take a look at the current list of lint rules:
https://dart-lang.github.io/linter/lints/
Currently, its at over 300 and growing. Are you going to type in all 300 rules each time you create a flutter app project? No, of course not! There are several lint plugins and I will show you each one and how to use them.
Effective Dart Lint Plugin
First up, is the Effective Dart plugin that sets out the lint rules group corresponding to the effective dart guidelines:
Effective Dart GuideLines https://dart.dev/guides/language/effective-dart
Effective Dart Lint Plugin https://pub.dev/packages/effective_dart
To use it you insert this line in your pub-spec in the dev_dependencies block:
dev_dependencies: effective_dart: ^1.3.1And you create a blank analysis_options.yaml file and put this at the top:
include: package:effective_dart/analysis_options.1.3.0.yamlSince you will use continuous builds at some point make sure to always include a version number in the file name URL include declaration.
Also in the top of your analysis_options.yaml file after the URL include put something like this:
analyzer:strong-mode:# my own setting 2-2-2021implicit-dynamic: false# Will become the default once non-nullable types land# https://github.com/dart-lang/sdk/issues/31410#issuecomment-510683629implicit-casts: falseerrors:# treat missing required parameters as a warning (not a hint)missing_required_param: warning# treat missing returns as a warning (not a hint)missing_return: warning# allow having TODOs in the codetodo: ignoreThe reason for the first strong mode setting is that on certain maps and list things you want to set an Object type rather than a pre-assign dynamic type. As the dart-type infrastructure gets more mature we will no longer have to set it that way.
And you can always modify how the feedback is outputted as you can see in my error handling block.
Next up, is the Pedantic Lint Plugin.
Pedantic Lint Plugin
The purpose of the Pedantic Plugin is not only to document the lint rules gorup that the Google Dart and Flutter teams use but to allow outside devs to use it as well:
pedantic https://pub.dev/packages/pedantic
The same way we use the effective dart lint plugin, in your pubspec:
dev_dependencies: pedantic: ^1.11.0And at the top of your analysis_options.yaml file:
include: package:pedantic/analysis_options.1.11.0.yamlanalyzer:strong-mode:# my own setting 2-2-2021implicit-dynamic: false# Will become the default once non-nullable types land# https://github.com/dart-lang/sdk/issues/31410#issuecomment-510683629implicit-casts: falseerrors:# treat missing required parameters as a warning (not a hint)missing_required_param: warning# treat missing returns as a warning (not a hint)missing_return: warning# allow having TODOs in the codetodo: ignoreNext up, is the plugin I use and it's called, lint.
Lint Plugin
The plugin I use, Lint is at:
It’s a more opinionated lint rule group by Pascal Welsch.
You install it the same way as the rest of the lint plugins by listing the dependency in your pubspec, like this:
dev_dependencies: lint: ^1.5.3And create a blank analysis_options.yaml file and insert this:
include: package:lint/analysis_options.yamlanalyzer:strong-mode:# my own setting 2-2-2021implicit-dynamic: false# Will become the default once non-nullable types land# https://github.com/dart-lang/sdk/issues/31410#issuecomment-510683629implicit-casts: falseerrors:# treat missing required parameters as a warning (not a hint)missing_required_param: warning# treat missing returns as a warning (not a hint)missing_return: warning# allow having TODOs in the codetodo: ignoreYou do not have to then list the full rules below, I do in mine as I have poor memory skills.
Conclusion
Static analysis of your project code helps you pro-actively prevent coding errors and code design errors. It sounds boring until you realize that it’s a guardrail that keeps you pointed in the right direction during code sprints on features, etc.
Resources
esources specific to the article:
flutter design and arch rosetta https://github.com/fredgrott/flutter_design_and_arch_rosetta
General Flutter and Dart resources:
Flutter Community Resources https://flutter.dev/community
Flutter SDK https://flutter.dev/docs/get-started/install
Android Studio IDE https://developer.android.com/studio
MS’s Visual Studio Code https://code.visualstudio.com/
Flutter Docs https://flutter.dev/docs
Dart Docs https://dart.dev/guides
Google Firebase Mobile Device TestLab https://firebase.google.com/docs/test-lab
Trademark Notice
Google LLC owns the following trademarks; Dart, Flutter, Android, Roboto, Noto. Apple Inc owns the trademarks iOS, MacOSX, Swift, and Objective-C. Apple Inc owns trademarks to their fonts of SF Pro, Sf Compact, SF mono, and New York. JetBeans Inc owns the trademarks of JetBeans, IntelliJ, and Kotlin. Oracle Inc owns the Java trademark. Microsoft Inc owns the trademarks of MS Windows OS and Powershell. Gradle is a trademark of Gradle Inc. The Git Project owns the trademark to Git. Linux Foundation owns the trademark of Linux. Smartphone OEM’s own trademarks to their mobile phone product names. Samsung owns the trademark to Tizen. To the best of my ability, I follow the brand and usage guidelines with the above-mentioned trademarks.
About Me, Fred Grott
I am on a different adventure of creating a Maker and Creator studio the bootstrap way in teaching Flutter Application Design and Development to you guys and gals.
My keybase profile is at: https://keybase.io/fredgrott




