Giovanni Minelli
Giovanni Minelli

Reputation: 126

Hot restart in Flutter: when and when not?

could someone explain me the hot reload and hor restart and when i don't have to do it (like changes in code that needs to perform a rebuild) and when i surely can. Both in Flutter web and mobile when debugging if i'm not sure that the changes i've made, affected the result, i close all and make a rebuild of the project... some insight in these two mode of "rapid-build" would make me more confident in what button i click during debug ;)

IDE: Android Studio 3.5.1

Upvotes: 1

Views: 4609

Answers (3)

Ben Butterworth
Ben Butterworth

Reputation: 28482

There's an official video for this now: Hot reload?! | Decoding Flutter

Hot reload (<1s), also know as 'stateful hot reload' should be used as often as possible. You need to remember in these 3 cases, you should do a hot restart (~10s) and lose the state of the application (current variables):

  1. Global variable initializers, outside a class
  2. Static field initializers
  3. The main() method of the app

An example:

int globalVariableNeedsHotRestart = 5; // <-- A global variable.

class StackOverflowWidget {
  static const staticFieldInitialized = Text("Hello"); // <-- Using the static keyword
}

main(List<String> args) {
  print(
      "This won't run until a hot restart. A hot reload won't do it."); // <-- Any code in main method
}

Upvotes: 1

nordine enn
nordine enn

Reputation: 88

if hot reload or hot restart dosen't work just run command flutter run and thats will work correcelty with command line

Upvotes: 0

Andr&#233; Junior
Andr&#233; Junior

Reputation: 234

This if from the official documentation of Flutter for Android Studio and IntelliJ.

Hot Reload vs. Hot Restart

Hot reload works by injecting updated source code files into the running Dart VM (Virtual Machine). This includes not only adding new classes, but also adding methods and fields to existing classes, and changing existing functions. A few types of code changes cannot be hot reloaded though:

  • Global variable initializers

  • Static field initializers

  • The main() method of the app

For these changes you can fully restart your application, without having to end your debugging session.

From: https://flutter.dev/docs/development/tools/android-studio#hot-reload-vs-hot-restart

And here goes another explanation.

Hot Reload:

Hot reload feature quickly compile the newly added code in our file and sent the code to Dart Virtual Machine. After done updating the Code Dart Virtual Machine update the app UI with widgets. Hot Reload takes less time then Hot restart. There is also a draw back in Hot Reload, If you are using States in your application then Hot Reload preservers the States so they will not update on Hot Reload our set to their default values.

Hot restart:

Hot restart is much different than hot reload. In Hot restart it destroys the preserves State value and set them to their default. So if you are using States value in your application then After every hot restart the developer gets fully compiled application and all the states will set to their defaults. The app widget tree is completely rebuilt with new typed code. Hot Restart takes much higher time than Hot reload.

I got this explanation from: https://flutter-examples.com/difference-between-hot-reload-and-hot-restart-in-flutter-dart/

Upvotes: 7

Related Questions