Optimization of Flutter Web App Start-up Time

Engineering

Flutter

Web

Summary

Flutter, an open-source SDK by Google, faces challenges with start-up times in web applications, often showing a blank screen during initialization. This article explores strategies that can significantly improve the initialization speed and user engagement by minimizing wait times and optimizing performance.

Key insights:
  • Flutter's web application start-up delay is due to multiple initializations steps including loading files and resources.

  • The "--release" option and tree shaking technique help reduce unnecessary code and minimize application size, enhancing start-up speed.

  • Compressing images and other assets effectively reduces load times without compromising quality.

  • Lazy loading techniques like ListView.builder and GridView.builder optimize the rendering process by loading only visible widgets initially.

  • Implementing a custom splash screen can significantly improve user experience by replacing the default blank screen with engaging visuals during load times.

Introduction

Flutter is an open source software development kit created by Google as a one-stop solution for building web, mobile, and desktop applications. However, when it comes to web applications, Flutter faces a major challenge in its start-up time. Upon initialization, Flutter takes a long time to load the user interface during which the user is met with a blank white screen. This article aims to explain the start-up time as well as identify techniques to improve user experience by optimizing Flutter load up time.

The initialization of Flutter involves several steps, adding to the load time. These include:

1. Loading the main dart file

2. Constructing the Service Worker structure

3. Loading resources (images, icons, etc.)

4. Constructing the Document Object Model before the interface is shown to the user.

Drawing upon the process of initializing the web application, several techniques can be employed in order to optimize the waiting time.

Methods to Optimize the startup time:

  1. Use the “--release” option when testing the application

When applications are built using “flutter build”, Flutter automatically applies the option mentioned. However, it must be added by the user when testing the application without building it. The “--release” tag allows for optimization of the application to improve performance and minimize the footprint size. For web applications, this means that Flutter is performing a technique called ‘Tree Shaking’

Tree Shaking is an optimization technique for file size used by Flutter that draws upon the concept that a program’s execution paths can be expressed as a tree of function calls. During the build process, unused code, libraries, and classes are removed from the application. Consequently, this results in faster initialization Flutter web applications as the size of the bundle is significantly reduced.

  1. Compressing Assets

Applying compression techniques on assets like images can help reduce the size of the web application, leading to quicker load times. This process involves reducing the file size without having a large impact on quality. Images can be compressed using formats like JPEG, PNG, and WebP, which offer a balance between quality and file size. Implementing compressing techniques on assets, which is often overlooked, is a powerful technique that developers should consider when building web applications. 

To achieve this, Flutter has a package called image_picker_for_web that takes an option named imageQuality: [X] with X being the percentage of quality. However, when applying compression techniques, striking a balance between file quality and file size is an important aspect to keep in mind as it directly affects two key factors: user experience and the web application performance.

  1. Lazy Loading

When your web application is large and features multiple widgets on screens, lazy loading is an effective strategy to use. During initialization, why not just load the widgets that the user can immediately see instead of loading the entire application - including widgets that are currently outside the user’s view? Flutter implements lazy loading through several mechanisms and widgets. These include: 

ListView.builder: Create a scrollable list of widgets that are only rendered in real time when the user scrolls onto them. 

GridView.builder: Create a scrollable grid of widgets that are rendered in real time.This is generally used for displaying items in two dimensional arrays like images.

Using these widgets in your web application can help reduce the initial load-up time by reducing the number of widgets that are required to be rendered at launch.

  1. Adding a Splash Screen

Using a custom load screen instead of a blank white screen while the web application is initializing can help improve the user experience. There are several tutorials available online that have implementations of these loading screens. Here is a high level step-by-step guide for implementing a custom splash screen:

1. In the Index.html file, add a div that holds the content of your custom splash screen inside the body of the html file.

2. Define CSS elements for the splash screen

3. Create a JavaScript file for the splash screen. Inside this file, listen for window.load event. When the event is triggered, call the _flutter.load.loadEntryPoint method and initialize the Flutter engine. Before running the application, make sure to remove the custom splash screen. 

4. Link the JavaScript file in the HTML file. 

Following these steps, your new custom splash screen should be ready to go!

Conclusion

The startup time of a Flutter web application can be optimized by implementing the strategies outlined in this article. By employing techniques like code optimization, asset compression, lazy loading, and the introduction of a custom splash screen, developers can improve the user experience while the web application initializes.. These optimizations not only enhance user satisfaction by reducing wait times but also contribute to the overall performance and efficiency of the web application.

References

https://medium.com/@hiroyamaguch/optimization-of-flutter-web-loading-with-3-files-d5d3c6794fca

https://medium.com/@michejin/understanding-tree-shaking-in-flutter-cffb9cbc8a8f#:~:text=What%20is%20Tree%20Shaking%3F,code%20contains%20only%20necessary%20code

https://docs.flutter.dev/testing/build-modes

https://pub.dev/packages/image_picker_for_web

https://docs.flutter.dev/perf/best-practices

Other Insights

Got an app?

We build and deliver stunning mobile products that scale

Got an app?

We build and deliver stunning mobile products that scale

Got an app?

We build and deliver stunning mobile products that scale

Got an app?

We build and deliver stunning mobile products that scale

Got an app?

We build and deliver stunning mobile products that scale

Our mission is to harness the power of technology to make this world a better place. We provide thoughtful software solutions and consultancy that enhance growth and productivity.

The Jacx Office: 16-120

2807 Jackson Ave

Queens NY 11101, United States

Book an onsite meeting or request a services?

© Walturn LLC • All Rights Reserved 2024

Our mission is to harness the power of technology to make this world a better place. We provide thoughtful software solutions and consultancy that enhance growth and productivity.

The Jacx Office: 16-120

2807 Jackson Ave

Queens NY 11101, United States

Book an onsite meeting or request a services?

© Walturn LLC • All Rights Reserved 2024

Our mission is to harness the power of technology to make this world a better place. We provide thoughtful software solutions and consultancy that enhance growth and productivity.

The Jacx Office: 16-120

2807 Jackson Ave

Queens NY 11101, United States

Book an onsite meeting or request a services?

© Walturn LLC • All Rights Reserved 2024

Our mission is to harness the power of technology to make this world a better place. We provide thoughtful software solutions and consultancy that enhance growth and productivity.

The Jacx Office: 16-120

2807 Jackson Ave

Queens NY 11101, United States

Book an onsite meeting or request a services?

© Walturn LLC • All Rights Reserved 2024

Our mission is to harness the power of technology to make this world a better place. We provide thoughtful software solutions and consultancy that enhance growth and productivity.

The Jacx Office: 16-120

2807 Jackson Ave

Queens NY 11101, United States

Book an onsite meeting or request a services?

© Walturn LLC • All Rights Reserved 2024