الانتقال إلى المحتوى
تطبيقات الموبايلFlutter· 6 دقائق للقراءة

Hot Reload Is Not a Fresh Start: What It Keeps and What It Skips

Hot reload runs new code against old state, so a screen that works after one hasn't been tested from a cold start.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • Flutter 3.x
محتويات المقال

A developer adds infinite scroll to the feed: one addListener call in initState, save, scroll down. Nothing loads. Hot reload "ignored" initState, so they move the line into build(), save again, and it works. Two weeks later, the backend team asks why every scroll near the bottom of the feed fires the same page request several times. The code was tested, just not the way it would run on a user's phone.

The common misconception

The belief: hot reload re-runs the app with the new code, only faster. So if the screen works after a hot reload, it will work when someone opens the app from scratch.

It's easy to believe because, for most UI work, that's exactly how it feels. You change a color or a padding in build(), save, and the screen updates in about a second without losing your place. The console reports something like Reloaded 1 of 448 libraries, which sounds like a complete reload. And on the web, "reload" has always meant a fresh start. The catch is in the part that isn't visible: what hot reload deliberately leaves alone.

What actually happens

Hot reload patches code, then rebuilds

According to Flutter's docs, a recompiles the libraries you changed, plus the main library and the libraries that lead to them, into kernel files and sends them to the Dart VM running your app. The VM loads the new code, and at that point no code has been re-executed yet. Then the framework rebuilds, re-lays out and repaints every existing widget and render object. Only code involved in rebuilding the widget tree runs again. It works only in debug mode.

New code, old state

Everything the app built up before the reload survives it. Put together with the docs' special cases, your code falls into four groups:

  • Runs again: every build() method and whatever it calls. Changed function bodies take effect the next time they're called, and changes to const values are always picked up.
  • Kept: State objects and their fields, the navigation stack, and global variables and static fields, which the docs say are treated as state and not reinitialized.
  • Not run again: main(), initState() for State objects that already exist, and initializers of globals and statics that already ran.
  • Not applied at all: native Kotlin, Java, Swift or Objective-C code, which needs a full restart, plus a few type-level changes such as turning an enum into a class or changing a generic type's parameters.

The docs also warn that when code changes affect your app's state, the data it works with after a reload might not match what it would have if it had run from scratch. You logged in an hour ago, opened three screens and filled a cache. Your new code now runs against all of that. A bug that only appears on a clean launch, with no user, an empty cache or a different first screen, stays hidden.

Bending code to fit hot reload

That's the feed bug. The listener in initState was correct, but the feed's State object already existed, so the reload never ran it. Moving the call to build() made it "work", and planted a leak:

Dart feed_screen.dart
class _FeedScreenState extends State<FeedScreen> {  final _scroll = ScrollController();  void _onScroll() {    if (_scroll.position.extentAfter < 300) widget.onNearEnd();  }  @override  Widget build(BuildContext context) {    // Moved here so hot reload "picks it up": one more listener per build    _scroll.addListener(_onScroll);    return ListView.builder(      controller: _scroll,      itemBuilder: (context, i) => ListTile(title: Text('Post $i')),    );  }}

ChangeNotifier's docs are explicit: adding a closure that's already registered adds another instance of it. So every build registers _onScroll again. In a widget test, after the screen had built five times, a single scroll called the listener five times. And since a hot reload rebuilds every widget, each save during development added one more. In the right version, initState runs once for each State object, as its docs say. To see a change there, you restart instead of rewriting the code.

Change you madeAfter a hot reloadTo see it
A build() method or code it callsApplied: the tree rebuildsHot reload
initState() or a State field's initial valueNot run for existing State objectsHot restart, or reopen the screen
main(), or what you pass to runAppNot runHot restart
A global or static variable's initializerOld value kept, unless it's constHot restart
Kotlin, Java, Swift or Objective-C codeNot appliedFull restart: stop and run again

How to apply it in your project

  1. Know which kind of change you just madeChanges inside build() are safe to hot reload. Changes to initState, main(), globals or statics need a hot restart, and native or plugin changes need a full restart.
  2. Never move setup code to make hot reload see itListeners, subscriptions and controllers belong in initState and get cleaned up in dispose. If hot reload doesn't show the change, restart; don't relocate the code into build().
  3. Walk the flow from a cold start before you call it doneHot restart, start from the first screen and go through the feature: logged out, empty cache, fresh install where it matters. That's the path your users take, not the one your debug session took.
  4. Judge speed and startup outside debug modeProfile scrolling and measure startup in profile or release mode on a physical device, never in a debug session.

Key takeaways

  • Hot reload injects new code and rebuilds the widget tree; it doesn't restart your app.
  • State objects, globals and statics keep their values, and main(), initState() and old initializers don't rerun.
  • Native code and some type changes need a hot or full restart to take effect.
  • "Works after a hot reload" means "works with this session's state"; confirm it with a hot restart.
  • Don't bend code to hot reload: setup stays in initState, and you restart to see it.

📚 المصادر

  1. Hot reload — Flutter docs
  2. Flutter's build modes — Flutter docs
  3. State.initState — Flutter API
  4. ChangeNotifier.addListener — Flutter API

التعليقات (0)