
Hot Reload Is Not a Fresh Start: What It Keeps and What It Skips
- كاتب موثّق
- Flutter 3.x
محتويات المقال
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
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
New code, old state
Runs again: every build()method and whatever it calls. Changed function bodies take effect the next time they're called, and changes toconstvalues are always picked up.Kept: Stateobjects 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()forStateobjects 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.
Bending code to fit hot reload
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:
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')), ); }}class _FeedScreenState extends State<FeedScreen> { final _scroll = ScrollController(); @override void initState() { super.initState(); _scroll.addListener(_onScroll); // once per State: hot restart to see it } @override void dispose() { _scroll.dispose(); super.dispose(); } void _onScroll() { if (_scroll.position.extentAfter < 300) widget.onNearEnd(); } @override Widget build(BuildContext context) => 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.
build() method or code it calls | ||
initState() or a State field's initial value | State objects | |
main(), or what you pass to runApp | ||
const | ||
How to apply it in your project
Know which kind of change you just made Changes inside build()are safe to hot reload. Changes toinitState,main(), globals or statics need a hot restart, and native or plugin changes need a full restart.Never move setup code to make hot reload see it Listeners, subscriptions and controllers belong in initStateand get cleaned up indispose. If hot reload doesn't show the change, restart; don't relocate the code intobuild().Walk the flow from a cold start before you call it done Hot 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. Judge speed and startup outside debug mode Profile 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. Stateobjects, globals and statics keep their values, andmain(),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.
التعليقات (0)