text
stringlengths
1
474
<topic_start>
Choosing which option to use
Choose the auto option (default) if you are optimizing for download size on
mobile browsers and optimizing for performance on desktop browsers.Choose the html option if you are optimizing download size over performance on
both desktop and mobile browsers.Choose the canvaskit option if you are prioritizing performance and
pixel-perfect consistency on both desktop and mobile browsers.<topic_end>
<topic_start>
Examples
Run in Chrome using the default renderer option (auto):Build your app in release mode, using the default (auto) option:Build your app in release mode, using just the CanvasKit renderer:Run your app in profile mode using the HTML renderer:
<topic_end>
<topic_start>Customizing web app initialization
You can customize how a Flutter app is initialized on the web
using the _flutter.loader JavaScript API provided by flutter.js.
This API can be used to display a loading indicator in CSS,
prevent the app from loading based on a condition,
or wait until the user presses a button before showing the app.The initialization process is split into the following stages:This page shows how to customize the behavior
at each stage of the initialization process.<topic_end>
<topic_start>
Getting started
By default, the index.html file
generated by the flutter create command
contains a script tag
that calls loadEntrypoint from the flutter.js file:info Note
In Flutter 2.10 or earlier,
this script doesn’t support customization.
To upgrade your index.html file to the latest version,
see Upgrading an older project.The loadEntrypoint function calls the onEntrypointLoaded callback
once the Service Worker is initialized, and the main.dart.js entrypoint
has been downloaded and run by the browser. Flutter also calls
onEntrypointLoaded on every hot restart during development.The onEntrypointLoaded callback receives an engine initializer object as
its only parameter. Use the engine initializer to set the run-time
configuration, and start the Flutter Web engine.The initializeEngine() function returns a Promise
that resolves with an app runner object. The app runner has a
single method, runApp(), that runs the Flutter app.<topic_end>
<topic_start>
Customizing web app initialization
In this section,
learn how to customize each stage of your app’s initialization.<topic_end>
<topic_start>
Loading the entrypoint
The loadEntrypoint method accepts these parameters:The serviceWorker JavaScript object accepts the following properties:<topic_end>
<topic_start>
Initializing the engine
As of Flutter 3.7.0, you can use the initializeEngine method to
configure several run-time options of the Flutter web engine through a
plain JavaScript object.You can add any of the following optional parameters:info Note
Some of the parameters described above might have been overridden
in previous releases by using properties in the window object.
That approach is still supported, but displays a deprecation
notice in the JS console, as of Flutter 3.7.0.<topic_end>
<topic_start>Engine configuration example
The initializeEngine method lets you pass any of the configuration
parameters described above to your Flutter app.Consider the following example.Your Flutter app should target an HTML element with id="flutter_app" and
use the canvaskit renderer. The resulting JavaScript code would resemble
the following:For a more detailed explanation of each parameter, take a look at the
“Runtime parameters” documentation section of the configuration.dart
file of the web engine.<topic_end>
<topic_start>Skipping this step
Instead of calling initializeEngine() on the engine initializer (and then
runApp() on the application runner), you can call autoStart() to
initialize the engine with its default configuration, and then start the app
immediately after the initialization is complete:<topic_end>
<topic_start>
Example: Display a progress indicator
To give the user of your application feedback
during the initialization process,
use the hooks provided for each stage to update the DOM:For a more practical example using CSS animations,
see the initialization code for the Flutter Gallery.<topic_end>
<topic_start>
Upgrading an older project
If your project was created in Flutter 2.10 or earlier,
you can create a new index.html file
with the latest initialization template by running
flutter create as follows.First, remove the files from your /web directory.Then, from your project directory, run the following:
<topic_end>
<topic_start>Display images on the web
The web supports the standard Image widget to display images.
However, because web browsers are built to run untrusted code safely,
there are certain limitations in what you can do with images compared
to mobile and desktop platforms. This page explains these limitations
and offers ways to work around them.<topic_end>
<topic_start>
Background
This section summarizes the technologies available
across Flutter and the web,
on which the solutions below are based on.<topic_end>
<topic_start>
Images in Flutter
Flutter offers the Image widget as well as the low-level
dart:ui/Image class for rendering images.
The Image widget has enough functionality for most use-cases.
The dart:ui/Image class can be used in
advanced situations where fine-grained control
of the image is needed.<topic_end>
<topic_start>
Images on the web
The web offers several methods for displaying images.
Below are some of the common ones:Each option has its own benefits and drawbacks.
For example, the built-in elements fit nicely among
other HTML elements, and they automatically take