Learn how to improve your code quality in flutter by defining how flutter analyzes your code.
Flutter is built on dart which is a statically typed language and now only a few months ago, allows null safety.But even with null safety in place bugs and small annoying problems will easily erupt so Dart has an inbuilt tool, the Dart Analysis Server, built on top of the analyzer package used to detect errors even before any line of code is run. This helps improve your code and reduces chances of bugs.But it does not provide enough flexibility and that’s where Analysis options come in.
So what are analysis options? Analysis options is a set of rules which help customize the dart analysis. The rules are written in a file, analysis_options in the project’s root folder.
Create a new project by opening your terminal in any folder and enter the following command flutter create new_project_name. Open the project once it is done generating using your latest favorite IDE. If you have an existing project you can open it using your preferred IDE also.
Create a new file in the lib folder called analysis_options.yaml and add the following rules. We will go through them to get a better understanding of what they mean.
The exclude rule is meant to instruct the analyzer not to check anything within the folder or any file that ends with a certain pattern.For example the rules shown above show two lines one with build and the other with an asterisk ending (*.g.dart). This means that the analyzer will skip any file within the lib folder that has the extension: .g.dart. Please note that files with the given extension are auto-generated and we would want to reduce on time the analyzer takes to go through them.In this case the build/** means the analyzer will skip all files within the build folder. This is where our flutter app is compiled to a bundle that can run on a device.
Produce an error every time there’s a function that does not return its type.
This is a mistake we overlook especially when writing if statements with multiple else if. This rule will produce an error for unreachable code.
Will check for return types on all functions. In case of new updates it will prevent a miss in case of a change.
Prevent installs of libraries that are meant for the web. Only allow libraries and plugins that are meant for android and IOS.
Ensure that all function and method parameters are added otherwise pop error.
Dart has a preferred naming system called the snake case system. This rule will give an error if a file created does not follow this naming system. The files are named with underscores as separators and all letters in lowercase.
Other naming systems include Kebab, Pascal case and Camel Case. You can read more here
View the following code to get a better understanding.
Invoke an error if a list or any class that extends the iterable to accept only a certain type any other unrelated to fail.
This checks for type before removing an item. Works on iterable.
This means if there are more than one return which would lead to dead code, the analyzer shows it as an error.
This will show an error when trying to compare two values of different type.
This rule ensures that there isn’t a line of code that is longer than 80 characters long. The code will need to be wrapped to the next line.
Ensures that only single quotes are used from importing files to initializing String properties.
Dart checks for the analysis options but you can also get analyzer to run by running the following command from the terminal within the project’s directory dart analyzer .