<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Brian Mwangi</title>
    <description>The latest articles on DEV Community by Brian Mwangi (@brianmwangi).</description>
    <link>https://dev.to/brianmwangi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F502204%2F16fb8bff-0aae-4a8d-9bf8-13b829971518.jpg</url>
      <title>DEV Community: Brian Mwangi</title>
      <link>https://dev.to/brianmwangi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/brianmwangi"/>
    <language>en</language>
    <item>
      <title>Cleaner code in Flutter:Getting Started</title>
      <dc:creator>Brian Mwangi</dc:creator>
      <pubDate>Fri, 16 Jul 2021 19:42:03 +0000</pubDate>
      <link>https://dev.to/brianmwangi/cleaner-code-in-flutter-getting-started-58ff</link>
      <guid>https://dev.to/brianmwangi/cleaner-code-in-flutter-getting-started-58ff</guid>
      <description>&lt;p&gt;Learn how to improve your code quality in flutter by defining how flutter analyzes your code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Overview
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are analysis Options
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting Started
&lt;/h3&gt;

&lt;p&gt;Create a new project by opening your terminal in any folder and enter the following command &lt;strong&gt;flutter create new_project_name&lt;/strong&gt;. 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.&lt;/p&gt;

&lt;h3&gt;
  
  
  Analysis Options - Adding Options
&lt;/h3&gt;

&lt;p&gt;Create a new file in the lib folder called &lt;strong&gt;analysis_options.yaml&lt;/strong&gt; and add the following rules. We will go through them to get a better understanding of what they mean.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  Exclude
&lt;/h4&gt;

&lt;p&gt;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 &lt;strong&gt;(*.g.dart)&lt;/strong&gt;. 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.&lt;/p&gt;

&lt;h4&gt;
  
  
  Errors
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;missing_return:&lt;/strong&gt;&lt;br&gt;
Produce an error every time there’s a function that does not return its type.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;dead_code&lt;/strong&gt;&lt;br&gt;
This is a mistake we overlook especially when writing if statements with multiple else if. This rule will produce an error for unreachable code.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;always_declare_return_types&lt;/strong&gt;&lt;br&gt;
Will check for return types on all functions. In case of new updates it will prevent a miss in case of a change.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;avoid_web_libraries_in_flutter&lt;/strong&gt;&lt;br&gt;
Prevent installs of libraries that are meant for the web. Only allow libraries and plugins that are meant for android and IOS.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;missing_required_param&lt;/strong&gt;&lt;br&gt;
Ensure that all function and method parameters are added otherwise pop error.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;file_names&lt;/strong&gt;&lt;br&gt;
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.&lt;br&gt;
Other naming systems include Kebab, Pascal case and Camel Case. You can read more &lt;a href="https://en.wikipedia.org/wiki/Naming_convention_(programming)"&gt;here&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;empty_statements&lt;/strong&gt;&lt;br&gt;
View the following code to get a better understanding.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;iterable_contains_unrelated_type&lt;/strong&gt;&lt;br&gt;
Invoke an error if a list or any class that extends the iterable to accept only a certain type any other unrelated to fail.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;list_remove_unrelated_type&lt;/strong&gt;&lt;br&gt;
This checks for type before removing an item. Works on iterable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;no_duplicate_case_values&lt;/strong&gt;&lt;br&gt;
This means if there are more than one return which would lead to dead code, the analyzer shows it as an error.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;unrelated_type_equality_checks&lt;/strong&gt;&lt;br&gt;
This will show an error when trying to compare two values of different type.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;lines_longer_than_80_chars&lt;/strong&gt;&lt;br&gt;
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.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;prefer_single_quotes:&lt;/strong&gt;&lt;br&gt;
Ensures that only single quotes are used from importing files to initializing String properties.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;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 &lt;strong&gt;dart analyzer .&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>codequality</category>
    </item>
  </channel>
</rss>
