Flutter is a mobile app SDK that allows you to create high-performance, cross-platform apps. Flutter includes a number of strong features that enable developers to create visually appealing and complicated user experiences. Flutter is highly extendable and comes with a variety of plugins that can be utilized during the development process.
For mobile applications, performance is critical. However, the number of missing frames and freezes affect the app's reliability, create a negative impression among customers, and eventually harm the organization. Overall, the performance of the Flutter program is satisfactory. Flutter is fast by default but we must avoid making several mistakes when writing the code in order for the application to work smoothly and quickly.
There is no guarantee that you will have bugs by using functions, but by using classes you are guaranteed to avoid them. By using functions, you expose yourself to bugs and miss out on some performance optimizations.
Prefer Reducing App Size
During the development process, it is very simple to add a large number of packages to your code. Applying ProGuard rules to your Android app is one of the best techniques for reducing APK size. In XCode for iOS, go to the App Thinning section, select All compatible device versions, then Rebuild from Bitcode and Strip Swift symbols. The .ipa file should then be signed and exported.
Prefer Const where Possible
To minimize unwanted rebuilds caused by parent widgets, we must use const as much as possible for our widgets. Using a const allows us to save CPU cycles. When setState calls are made, the widget does not change; the const keyword prevents the widget from rebuilding.
Avoid Rendering Frames
Rendering at 60 frames per second on Android devices and 60 frames per second on iOS devices is recommended. This ensures that the application does not consume battery power or slow down the smartphone while running in the background.
Prefer nil over Widgets
When you wish to show nothing with little impact on performance, use nil package in the widget tree instead of a widget such as Container().
Prefer using Keys with Widgets
Prefer itemExtent in ListView
We have a list of 5,000 elements. When we hit the button, we will jump to the last part, but this will be a slow process because we are allowing the children to choose their own size. To prevent this, we must use the itemExtent parameter, which allows the scrolling mechanism to save effort by knowing the extent of the children. We receive an immediate jump with that tiny tweak, with no delays.
Prefer Dart Style
In UpperCamelCase, names capitalize the first letter of each word.
In lowerCamelCase, names capitalize the first letter of each word, with the exception of the first.
If one of the RenderObjects in the same layer is designated as dirty, Flutter may repaint it. When a RenderObject has to be repainted, it notifies its nearest ancestor. The ancestor performs the same to the ancestor, maybe until the root RenderObject is reached. RepaintBoundary can detach ancestor render objects from descendant render objects, repainting just the subtree whose content changes.
Prefer Widgets over Methods
When we have a big design, we normally split it up using methods for each widget. Internally, when we make a change and refresh the full widget, it also refreshes the widgets that we have within the method, causing us to waste CPU cycles. We should transform those methods into a stateless Widget.
Prevent rebuilding all Widgets
Rebuilding StatefulWidget using setState, which rebuilds the entire widget again, but this is not a good practice; we only need to rebuild what we want to update. This can be accomplished using a state management package such as flutter bloc and provider, among others. However, it is also possible to do so without the use of an additional package, namely by utilizing the ValueNotifier and ChangeNotifier classes provided by the flutter framework.
Prefer async/await Keywords
If you wish to minimize development time, build strong code to avoid logical errors, and increase code readability, use null-check operators, and nullable operators.
Prefer String Interpolation
Using the operator + to do string operations instead, use string interpolation, which enhances the readability of your code and minimizes the likelihood of errors.
Prefer Stateless Widgets
To build our app, we use far too many stateful widgets. While stateful widgets require a lot of resources in order to rebuild the complete widget. Breaking down large widgets into several stateless widgets is one way to avoid heavy rebuild.
Prefer Optimizing Animations
Use fewer animations as much as possible. Instead of animating the user on the screen, for example, use a loading indicator to indicate that work is being done.