Cleaner code in Flutter: Getting Started

This article has been written by the awesome Brian Mwangi. Check it out on dev.to here!

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.

 
analyzer:
  exclude:
    - build/**
    - lib/**.g.dart
  errors: 
    missing_return: error
    dead_code: error
    always_declare_return_types: error
    avoid_web_libraries_in_flutter: error
    missing_required_param: error
    file_names: error
    camel_case_type: error
    empty_statements: error
    iterable_contains_unrelated_type: error
    list_remove_unrelated_type: error
    no_duplicate_case_values: error
    unrelated_type_quality_checks: error
    lines_longer_than_80_chars: warning
    prefer_single_quotes: error
  
    
 

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

  • missing_return:
    Produce an error every time there’s a function that does not return its type.
  • 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.
 
Future<Recipies> getRecipies(String? timeOfDay) {
  if(timeOfDay == "noon") {
  return foodApi.getAllNoonFood(); 
  } else {
    return foodApi.getAllMorningFood();
  }
  // This code will never execute
  return foodApi.getAllEveningFood();
}
  • 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.
  • 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.
  • missing_required_param
    Ensure that all function and method parameters are added otherwise pop error.
  • 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
  • empty_statements
    View the following code to get a better understanding.
     
// Bad practive
if(recipe.forLunch());
  getIngredients();

// Once it is formatted by dart formatter
if(recipe.forLunch()) ;
getIngredients();

//Good practice
if(recipe.forLunch())
  getIngredients();
  • 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.
  • list_remove_unrelated_type
    This checks for type before removing an item. Works on iterable.
  • 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.
  • unrelated_type_equality_checks
    This will show an error when trying to compare two values of different type.
  • 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.
  • 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 .

Add a Comment

Your email address will not be published. Required fields are marked *