Flutter – Best Practices

Defining the App Architecture Clearly:

Flutter architecture is divided into three layers:

  • Presentation layer
  • Business logic layer
  • Data layer.

A clearly defined architecture is a crucial prerequisite to making Flutter app development an easy process.

Usually, developers use BLoC architecture to implement the Flutter architecture in app development.

Flutter BLoC Best Practices:

By using the Flutter BLoC, developers can handle all possible states of the application with ease, which is a critical requirement for any mobile app. It manages all the states, It is flexible from simple to complex designs.

Different BLoC widgets can help perform different purposes. Flutter BLoC package provides four classes, which are:

  • BLoC provider – Create a cupid or BLoC.
  • BLoC listener – To respond to any change in the state of the BLoC.
  • BLoC builder – Building and rebuilding the sub-tree when changes occur.
  • BLoC consumer – Re-Build the UI.

 Execute Tests for Critical Functionalities:

Automated testing is a faster and more efficient way of testing software. It involves using software tools to create and run test cases automatically. In the case of Flutter, which targets multiple platforms, automated testing is even more crucial as it can help save a considerable amount of time and effort.

Aside from unit testing, integration testing ensures that all components of the software system work seamlessly together. 

 Instead of Containers, Use SizedBoxes:

You will need to use placeholders in many situations. One such example is

return _isNotLoaded ? Container() : YourAppropriateWidget();

The Container() expands to fit the constraints set by the parent and is not a constant constructor.

On the other hand, SizedBox is a const function [native code] that creates a fixed-size box. The width and height parameters can be null to indicate that the box’s size should not be constrained.

Example of SizedBox:

 return _isNotLoaded ? SizedBox() : YourAppropriateWidget();

Utilizing Streams Only When Necessary:

 If you don’t have an excellent implementation process, using streams can result in extra CPU and memory consumption.

Memory leaks can also occur if the streams are not properly closed. Therefore, it is best to keep the use of streams limited to essential tasks when constructing a Flutter app.

As an alternative, ChangeNotifier can be used for reactive UI while the BLoC library offers enhanced features and improved resource efficiency with a straightforward interface for creating user interfaces.

Instead of “Methods”, Refactor the Code Into “Widgets”:

This approach will provide you with the convenience of all widget lifecycle features and optimizations offered by the framework.

Fewer lines of code and simpler builds can be achieved with this method, while preventing unnecessary rebuilds

Use the Raw String:

A raw string treats backslashes (/) as literal characters. Backslashes are used as escape characters in normal strings.

//Do
 var s = r'This is demo string and $';

 //Do not
 var s = 'This is demo string \ and $';

Utilizing the “Dart Code Metrics”:

 This is a static tool for analyzing the code; it helps developers improve code quality and monitor it.

To effectively use the Dart Code Metrics tool, there are certain tasks that developers should carry out. For example, they should aim to use single widgets for each file and extract callbacks. By doing this, developers can make their code more modular and easier to read, understand, and maintain.

Following Proper Naming Conventions:

  • Use snake_case (lowercase with underscores) for libraries, directories, packages, and source files.
  • Start private variable names with underscores.
  • Use lowerCamelCase for constants, variables, parameters, and named parameters.
  • Use UpperCamelCase for classes, types, extension names, and enums.
  • Always use clear and meaningful names to improve code readability.
  • Following these naming conventions may seem like extra work at first, but it can save time in the long run by making your code easier to read and review.

Use of const Constructor:

It helps to reduce the garbage collector’s workload, though it may not seem relevant initially.

Leave a comment

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