Open In App

TreeShaking in Flutter

Last Updated : 11 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Flutter application are usually characterized with large APK size hence optimising application size has always been a matter of concern for Flutter developers. Flutter follows the mechanism of Treeshaking to reduce the final packet size after compilation of code. Have ever seen a gardener shaking the tree and collecting the dried leaves, similar logic is used by Flutter Engine to reduce the app size by eliminating the died part of the code.

What is Treeshaking in Flutter?

Treeshaking eliminates unused functions, libraries, and widgets from the project to minimize the size of the final APK. When you build the APK using release mode, dart Treeshaking features automatically removes the unused code from the project. The Dart compiler employs tree shaking in release mode to ensure that only the necessary parts of the code are included in the application, which reduces APK size and improves performance.

Treeshaking


Think of your Flutter application as a dependency tree, where the root of the tree is MyApp, and branches are various widgets, functions, and libraries. Any unused branches (widgets or functions) are pruned during the compilation process. Suppose a splash screen or a widget is not called in the final product but is still present in the project's code. This will increase the app size unnecessarily hence elimination of unused widget and code is necessary for building an efficient app.

How Treeshaking in Flutter works?

The Flutter Engine compiles the dart code to build application. The compiler marks the widget/function/plugins that are called within the project, the unused or unreferenced parts of the code, such as plugins, functions, or assets, are discarded. As the code is compiled, it forms a tree of function and widget that are called in the code. You can visualize the widget tree of any simple application. The Flutter engine determines which files, functions, or widgets are not connected to the root of the widget tree and eliminates them. Assets that are imported but not used in the application are detected and removed during tree shaking. Treeshaking will remove such asset from the application.

Importance of Treeshaking

  • Performance : Treeshaking improves the overall performance of the app. User experience becomes smooth and smaller apps load faster.
  • Maintenance : As the unused code is discarded, developers can easily maintain annd update the code.
  • Memory Usage : The project with smaller lines of code will efficiently occupy memory.
  • APK size : Treeshaking is the most effective way of reducing the app size by removing unwanted features.
  • Fast loading : After Treeshaking app loads faster and can be downloaded quicker.

Conclusion

Treeshaking is a significant step in optimizing the app size and performance. As the demand for cross platform application increases Treeshaking has become a major game changer in maitaining efficient size of the application across all the devices. Dart compiler follows the mechanism to shake off the unused part of the code and developer should master the strategy to enhance the proper functioning of Treeshaking.


Next Article
Article Tags :

Similar Reads