Best Practises For Better Flutter Code

  • Don’t make costly operations in your build methods
  • Do nothing else than building your UI in your build method; adding costly operations (like async calls or too big calculations) can lead to performance losses or unwanted behaviors.

  • Split Large Widgets into Smaller Widgets
  • In Flutter, your widget tree can become pretty large pretty quickly. In order to make your code readable and easy to manage, it’s advisable to separate such large widgets into a separate file.

  • Use Const Widgets
  • Whenever you have widgets that don’t change when the state changes, you should declare them as constants. This will prevent them from being rebuilt, hence improving performance.

    This is similar to caching widgets that won’t get rebuilt. Some of the widgets that can be declared as const include Text, Padding, and Icons, to mention a few. If the widgets contain dynamic information, they shouldn’t be declared as constants.

  • Use packages only when necessary
  • Packages can be a good choice when you want to speed up your building process and pub has a ton of them. Nonetheless, they can have significant effects on your app performances when they are used without prior study.

  • Dealing with lists
  • Use the default constructor ListView for rendering small lists of known size; for longer lists, use the ListView.builder constructor which renders list elements as users scroll to them.

    This is is also applicable when working with GridView that have a large or infinite number of widgets.

  • Remove Unused Resources
  • Especially when you’re ready to publish your application, you’ll need to remove resources that you aren’t using in your application. These could be image assets, for example. Removing unused resources and compressing images will help you reduce the size of your application.








Sources :

Flutter Development Best Practices

Flutter app development good practices