Flutter: Best Practices and Tips

Follow best practice to improve the quality of code and productivity

Kinjal Dhamat
Flutter Community
3 min readMay 2, 2020

--

Photo by Negative Space from Pexels

Best practices are professional standards that are acceptable in a field and it’s very important for any programing language to improve code quality, readability, maintainability, and robustness.

Let’s explore some best practices for designing and developing the Flutter app.

Naming convention:

Classes, enums, typedefs, and extensions name should in UpperCamelCase.

Libraries, packages, directories, and source files name should be in snake_case(lowercase_with_underscores).

Variables, constants, parameters, and named parameters should be in lowerCamelCase.

Use relative imports for files in lib

When use relative and absolute imports together then It is possible to create confusion when the same class gets imported from two different ways. To avoid this case we should use a relative path in the lib/ folder.

Specify types for class member

Always specify the type of member when its value type is known. Avoid using var when possible.

Avoid using as instead, use is operator

Usually, The as cast operator throws an exception if the cast is not possible. To avoid an exception being thrown, one can use is.

Use if condition instead of conditional expression

Many times we need to render a widget based on some conditions in Row and Column. If conditional expression return null in any case then we should use if condition only.

Use ?? and ?. operators

Prefer using ?? (if null) and ?. (null aware) operators instead of null checks in conditional expressions.

Use spread collections

When existing items are already stored in another collection, spread collection syntax leads to simpler code.

Use Cascades Operator

If we want to perform a sequence of operations on the same object then we should use the Cascades(..) operator.

Use raw string

A raw string can be used to avoid escaping only backslashes and dollars.

Don’t explicitly initialize variables null

In Dart, the variable is automatically initialized to null when its value is not specified, so is adding null is redundant and unneeded.

Use expression function bodies

For functions that contain just one expression, you can use an expression function. The => (arrow) notation is used for expression function.

Avoid print() calls

print()anddebugPrint()both are used for logging in to the console. If you are use print()and output is too much at once, then Android sometimes discards some log lines. To avoid this, use debugPrint(). If you log data has too much data then use dart:developer log(). This allows you to include a bit more granularity and information in the logging output.

Avoid using leading underscore for local identifiers that aren’t private.

Dart uses a leading underscore(_) in an identifier to mark members and top-level declarations as private. There is no concept of. private using leading underscore(_) for local variables, parameters, local functions, or library prefixes.

Use interpolation to compose strings

Use interpolation to make string cleaner and shorter rather than long chains of + to build a string.

Don’t create a lambda when a tear-off will do

If we have a function that invokes a method with the same arguments as are passed to it, you don’t need to manually wrap the call in a lambda.

Use async/await overusing futures callback

Asynchronous code is hard to read and debug. The async/await syntax improves readability.

Split widgets into sub Widgets.

When setState() is called on a State, all descendent widgets will rebuild. Therefore, Split the widget into small widgets so the setState() call to the part of the subtree whose UI actually needs to change.

Use ListView.builder for a long list

When working with infinite lists or very large lists, it’s usually advisable to use a ListView builder in order to improve performance.

Default ListView constructor builds the whole list at once. ListView.builder creates a lazy list and when the user is scrolling down the list, Flutter builds widgets on-demand.

Use Const in Widgets

The widget will not change when setState call we should define it as constant. It will prevent the widget to rebuild so it improves performance.

I hope this gives you a few insights to make your Flutter code more readable while also improving your app’s performance. Happy coding!

--

--

Kinjal Dhamat
Flutter Community

Flutter | FlutterFlow | Dart | Android | Kotlin | React Native | Node | Lead Software Engineer