Flutter best practices

CodingWithTashi
8 min readJul 16, 2022

During your programming journey, you will build hell lots of applications irrespective of any technology that you use, and getting these things done is one thing but doing it the right way is what matters.

Flutter best practices

Hello guys, this is CodingWithTashi and in this shitty post, we are going to discuss some of the best practices that you can apply while writing flutter/dart code.

[Background story]

So the other day I was going through one of my old flutter application source codes (App link here, sorry source code is not open source 😊). It took me some time to go through each module as the code was messy and unstructured.

But in between, I realized how much I have improved on flutter over time. So I decided to refactor my application and here are the lists of mistakes that I have made but you can avoid them.

New comers: Those of you who don’t know me. I write shitty post on programming and share my flutter journey from time to time. So if you are someone who is interested, make sure to follow and subscribe to my medium.

Alright so, If you are in rush you can consider these bullet points and leave, otherwise, feel free to read the explanation with an example.

  • Use the const keyword whenever possible
  • Define constant with leading “k”
  • Use commas to intend your code
  • Follow naming convention
  • Use the right widget
  • Use private variable/method whenever possible
  • Proper use of setState()
  • Avoid deep trees instead create a separate widget
  • Avoid method implementation within widget instead create a separate method
  • Avoid using nullable unless it is nullable
  • Use cascade (..)
  • Use spread operator (…)
  • define theme/route in a separate file
  • avoid using hardcoded strings (internationalized your app)
  • avoid using hardcoded style, decoration, etc.
  • Use relative import over package import within your app
  • Use a stateless widget whenever it is possible

--

--

CodingWithTashi

Native Android | Flutter Developer | Content Creator | Guitar Enthusiast