
# Hot reload

A hot reload (`r` in `flutter run`, saving in the IDE) puts the new code into the running app without starting it again: `main` does not run, and the objects in memory stay. For beans that is a problem. The application context was built from the old generated code, so a new `@Component` would be missing and a changed one would keep its old dependencies.

`SpringApplicationWidget` takes care of it, as Spring Boot DevTools' *restart* does. It is on by default and needs no setup.

## Why

Without it, every change to a bean or to a configuration file would need a hot restart (`R`), which loses the navigation, the scroll positions and the app process. With it, a change of a method body keeps everything, and a change of beans or files restarts only the context.

## How it decides

Flutter calls the root widget's `reassemble()` on every hot reload. There the widget compares the fingerprint of the generated code (`lib/spring_gen/main.spring.debug.dart`, compiled in debug and profile builds only) with the one the running context was built from.

**Only method bodies changed: the context stays.** The fingerprint is the same, so nothing restarts. Dart already runs the new method bodies, and the beans keep their state.

```dart
@ChangeNotifierComponent()
class Counter {
  int value = 0;
  void increment() => value += 2; // was `value++`: after `r` the next tap
                                  // adds 2, and the count so far is kept
}
```

**Beans, annotations or `lib/resources/` changed: the context restarts.** With `dart run build_runner watch` running, the generator rewrites `main.spring.dart` a moment after you save. The next hot reload sees a new fingerprint, and the widget:

1. closes the running context (`@PreDestroy` methods run, lifecycle beans stop, `ContextClosedEvent`);
2. starts the application again with the new beans and configuration files. The `ApplicationRunner`s do not run again, unless `rerunRunnersOnReload: true`: the app did not really start again;
3. builds the widgets below it afresh, their `State`s included, so no widget keeps a bean of the closed context. Navigation and scroll positions start over, as after a hot restart, but the app process (and anything outside the context) stays.

```dart
// Added while the app runs; `build_runner watch` regenerates, then `r`:
@Component()
class Clock {
  DateTime now() => DateTime.now();
}

// and in a widget:
Text('${context.bean<Clock>().now()}'); // found: the context restarted
```

```yaml
# application.yml: no code changed, still a restart with the new value
shop:
  page-size: 40
```

## What a hot reload cannot do

- Without `build_runner watch` the generated code stays old: a new bean is not found until you run the generator and reload again.
- A change Flutter itself cannot hot reload (a changed `main`, an enum turned into a class, ...) needs a hot restart (`R`), which starts everything from `main`.
- When the restart of the context fails (a bean that throws, a bad property), the failure screen shows the error. Fix the code and reload again, and the widget starts the application anew.

## Turning it off

A context restart drops the state of every bean. To keep the context as it is after every hot reload (and restart by hand with `R` when beans changed), set Spring Boot DevTools' switch:

```yaml
spring:
  devtools:
    restart:
      enabled: false
```

| Key | Type | Default | Meaning |
|---|---|---|---|
| `spring.devtools.restart.enabled` | bool | `true` | A hot reload restarts the context when the generated code changed: beans, annotations or `lib/resources/`. `false` keeps the context and the beans' state until a hot restart |

| `SpringApplicationWidget` parameter | Default | Meaning |
|---|---|---|
| `rerunRunnersOnReload` | `false` | Whether `ApplicationRunner` and `CommandLineRunner` beans run again after a hot reload restarts the context |

Release and profile builds have no hot reload, so none of this happens there.

## In Spring (Java)

Spring Boot DevTools restarts the application when the classpath changes, with two class loaders. Here the generated code's fingerprint plays that role, and only the context restarts, not the app.

## See also

- [SpringApplicationWidget](application-widget.md)
- [The app lifecycle](lifecycle.md)
- [How the generation works](../spring-core-dart-generator/how-it-works.md)
