Cleaner code in Flutter:Getting Started

Learn how to improve your code quality in flutter by defining how flutter analyzes your code.

Overview

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.

What are analysis Options

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.

Getting Started

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.

Analysis Options - Adding Options

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.

Exclude

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.

Errors

  1. missing_return:
    Produce an error every time there’s a function that does not return its type.

  2. dead_code
    This is a mistake we overlook especially when writing if statements with multiple else if. This rule will produce an error for unreachable code.

  3. always_declare_return_types
    Will check for return types on all functions. In case of new updates it will prevent a miss in case of a change.

  4. avoid_web_libraries_in_flutter
    Prevent installs of libraries that are meant for the web. Only allow libraries and plugins that are meant for android and IOS.

  5. missing_required_param
    Ensure that all function and method parameters are added otherwise pop error.

  6. file_names
    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

  7. empty_statements
    View the following code to get a better understanding.

  8. iterable_contains_unrelated_type
    Invoke an error if a list or any class that extends the iterable to accept only a certain type any other unrelated to fail.

  9. list_remove_unrelated_type
    This checks for type before removing an item. Works on iterable.

  10. no_duplicate_case_values
    This means if there are more than one return which would lead to dead code, the analyzer shows it as an error.

  11. unrelated_type_equality_checks
    This will show an error when trying to compare two values of different type.

  12. lines_longer_than_80_chars
    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.

  13. prefer_single_quotes:
    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 .

36