Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

docs.flutter.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.

Learn more

Flutter 3.41 is live! Check out theFlutter 3.41 blog post!

Hot reload

Speed up development using Flutter's hot reload feature.

Flutter's hot reload feature helps you quickly and easily experiment, build UIs, add features, and fix bugs. Hot reload works by injecting updated source code files into theDart runtime. After the Dart runtime updates classes with the new versions of fields and functions, the Flutter framework automatically rebuilds the widget tree, allowing you to quickly view the effects of your changes.

Hot reload GIF
A demo of hot reload in DartPad

How to perform a hot reload

#

To hot reload a Flutter app:

  1. Run the app from a supportedFlutter editor or a terminal window. Either a physical or virtual device can be the target.Only Flutter apps in debug mode can be hot reloaded or hot restarted.

  2. Modify one of the Dart files in your project. Most types of code changes can be hot reloaded; for a list of changes that require a hot restart, seeSpecial cases.

  3. If you're working in an IDE/editor that supports Flutter's IDE tools and hot reload on save is enabled, selectSave All (cmd-s/ctrl-s), or click the hot reload button on the toolbar.

    To enable hot reload on save

    From your preferred IDE, enable autosave and hot reloads on save.

    VS Code

    Add the following to your.vscode/settings.json file:

    json
    "files.autoSave":"afterDelay","dart.flutterHotReloadOnSave":"all",

    Android Studio and IntelliJ

    • OpenSettings > Tools > Actions on Save and selectConfigure autosave options.

      • Check the option toSave files if the IDE is idle for X seconds.
      • Recommended: Set a small delay duration. For example, 2 seconds.
    • OpenSettings > Languages & Frameworks > Flutter.

      • Check the option toPerform hot reload on save.

    If you're running the app at the command line usingflutter run, enterr in the terminal window.

After a successful hot reload operation, you'll see a message in the console similar to:

The app updates to reflect your change, and the current state of the app is preserved. Your app continues to execute from where it was prior to run the hot reload command. The code updates and execution continues.

What is the difference between hot reload, hot restart, and full restart?

  • Hot reload loads code changes into the VM or the browser, and re-builds the widget tree, preserving the app state; it doesn't rerunmain() orinitState(). (⌘\ in Intellij and Android Studio,⌃F5 in VSCode)
  • Hot restart loads code changes into the VM or the browser, and restarts the Flutter app, losing the app state. On the web, this can restart the app without a full page refresh. (⇧⌘\ in IntelliJ and Android Studio,⇧⌘F5 in VSCode)
  • Full restart restarts the iOS, Android, or web app. This takes longer because it also recompiles the Java / Kotlin / Objective-C / Swift / JavaScript code. On the web, it also restarts the Dart Development Compiler. There is no specific keyboard shortcut for this; you need to stop and start the run configuration.

Flutter web now supports hot restart andhot reload.

Android Studio UI
Controls for run, run debug, hot reload, and hot restart in Android Studio

A code change has a visible effect only if the modified Dart code is run again after the change. Specifically, a hot reload causes all the existing widgets to rebuild. Only code involved in the rebuilding of the widgets is automatically re-executed. Themain() andinitState() functions, for example, are not run again.

Special cases

#

The next sections describe specific scenarios that involve hot reload. In some cases, small changes to the Dart code enable you to continue using hot reload for your app. In other cases, a hot restart, or a full restart is needed.

An app is killed

#

Hot reload can break when the app is killed. For example, if the app was in the background for too long.

Compilation errors

#

When a code change introduces a compilation error, hot reload generates an error message similar to:

Hot reload was rejected:'/path/to/project/lib/main.dart': warning: line 16 pos 38: unbalanced '{' opens here  Widget build(BuildContext context) {                                     ^'/path/to/project/lib/main.dart': error: line 33 pos 5: unbalanced ')'    );    ^

In this situation, simply correct the errors on the specified lines of Dart code to keep using hot reload.

CupertinoTabView's builder

#

Hot reload won't apply changes made to abuilder of aCupertinoTabView. For more information, seeIssue 43574.

Enumerated types

#

Hot reload doesn't work when enumerated types are changed to regular classes or regular classes are changed to enumerated types.

For example:

Before the change:

dart
enumColor{red,green,blue}

After the change:

dart
classColor{Color(this.i,this.j);finalinti;finalintj;}

Generic types

#

Hot reload won't work when generic type declarations are modified. For example, the following won't work:

Before the change:

dart
classA<T>{T?i;}

After the change:

dart
classA<T,V>{T?i;V?v;}

Native code

#

If you've changed native code (such as Kotlin, Java, Swift, or Objective-C), you must perform a full restart (stop and restart the app) to see the changes take effect.

Previous state is combined with new code

#

Flutter's stateful hot reload preserves the state of your app. This approach enables you to view the effect of the most recent change only, without throwing away the current state. For example, if your app requires a user to log in, you can modify and hot reload a page several levels down in the navigation hierarchy, without re-entering your login credentials. State is kept, which is usually the desired behavior.

If code changes affect the state of your app (or its dependencies), the data your app has to work with might not be fully consistent with the data it would have if it executed from scratch. The result might be different behavior after a hot reload versus a hot restart.

Recent code change is included but app state is excluded

#

In Dart,static fields are lazily initialized. This means that the first time you run a Flutter app and a static field is read, it's set to whatever value its initializer was evaluated to. Global variables and static fields are treated as state, and are therefore not reinitialized during hot reload.

If you change initializers of global variables and static fields, a hot restart or restart the state where the initializers are hold is necessary to see the changes. For example, consider the following code:

dart
finalsampleTable=[Table(children:const[TableRow(children:[Text('T1')]),],),Table(children:const[TableRow(children:[Text('T2')]),],),Table(children:const[TableRow(children:[Text('T3')]),],),Table(children:const[TableRow(children:[Text('T4')]),],),];

After running the app, you make the following change:

dart
finalsampleTable=[Table(children:const[TableRow(children:[Text('T1')]),],),Table(children:const[TableRow(children:[Text('T2')]),],),Table(children:const[TableRow(children:[Text('T3')]),],),Table(children:const[TableRow(children:[Text('T10')],// modified),],),];

You hot reload, but the change is not reflected.

Conversely, in the following example:

dart
constfoo=1;finalbar=foo;voidonClick(){print(foo);print(bar);}

Running the app for the first time prints1 and1. Then, you make the following change:

dart
constfoo=2;// modifiedfinalbar=foo;voidonClick(){print(foo);print(bar);}

While changes toconst field values are always hot reloaded, the static field initializer is not rerun. Conceptually,const fields are treated like aliases instead of state.

The Dart VM detects initializer changes and flags when a set of changes needs a hot restart to take effect. The flagging mechanism is triggered for most of the initialization work in the above example, but not for cases like the following:

dart
finalbar=foo;

To updatefoo and view the change after hot reload, consider redefining the field asconst or using a getter to return the value, rather than usingfinal. For example, either of the following solutions work:

dart
constfoo=1;constbar=foo;// Convert foo to a const...voidonClick(){print(foo);print(bar);}
dart
constfoo=1;intgetbar=>foo;// ...or provide a getter.voidonClick(){print(foo);print(bar);}

For more information, read about thedifferences between theconst andfinal keywords in Dart.

Recent UI change is excluded

#

Even when a hot reload operation appears successful and generates no exceptions, some code changes might not be visible in the refreshed UI. This behavior is common after changes to the app'smain() orinitState() methods.

As a general rule, if the modified code is downstream of the root widget'sbuild() method, then hot reload behaves as expected. However, if the modified code won't be re-executed as a result of rebuilding the widget tree, then you won't see its effects after hot reload.

For example, consider the following code:

dart
import'package:flutter/material.dart';voidmain(){runApp(MyApp());}classMyAppextendsStatelessWidget{constMyApp({super.key});@overrideWidgetbuild(BuildContextcontext){returnGestureDetector(onTap:()=>print('tapped'));}}

After running this app, change the code as follows:

dart
import'package:flutter/widgets.dart';voidmain(){runApp(constCenter(child:Text('Hello',textDirection:TextDirection.ltr)));}

With a hot restart, the program starts from the beginning, executes the new version ofmain(), and builds a widget tree that displays the textHello.

However, if you hot reload the app after this change,main() andinitState() are not re-executed, and the widget tree is rebuilt with the unchanged instance ofMyApp as the root widget. This results in no visible change after hot reload.

How it works

#

When hot reload is invoked, the host machine looks at the edited code since the last compilation. The following libraries are recompiled:

  • Any libraries with changed code
  • The application's main library
  • The libraries from the main library leading to affected libraries

The source code from those libraries is compiled intokernel files and sent to the mobile device's Dart VM.

The Dart VM re-loads all libraries from the new kernel file. So far no code is re-executed.

The hot reload mechanism then causes the Flutter framework to trigger a rebuild/re-layout/repaint of all existing widgets and render objects.

Was this page's content helpful?

Unless stated otherwise, the documentation on this site reflects Flutter 3.38.6. Page last updated on 2025-11-26.View source orreport an issue.


[8]ページ先頭

©2009-2026 Movatter.jp