You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter热重载后更新不生效?修改代码需重启应用求解

Fixing Hot Reload Not Reflecting Dialog Changes in Flutter

Hey there! I totally get the frustration of tweaking your code, hitting Hot Reload, and seeing nothing change in the simulator—let's figure out why this is happening and fix it quickly.

The Root Cause

The issue here is that you've defined leadDialog as a global variable that's initialized once when your app starts up. Flutter's Hot Reload works by re-running your widget's build() method and recompiling updated Dart code, but it doesn't re-initialize existing global variable instances. That means your original Dialog instance stays in memory, even after you modify its code—so your changes never show up until you restart the app.

The Fix

Instead of creating a global Dialog instance, move the Dialog creation into a method (or directly inside your showDialog builder) so that a fresh instance is created every time the widget builds (including after Hot Reload). Here's how to adjust your code:

import 'package:flutter/material.dart';

class YourLeaderboardScreen extends StatelessWidget {
  // Create a method that returns your Dialog
  Dialog _buildLeadDialog() {
    return Dialog(
      child: Container(
        color: Colors.white,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Padding(
              padding: EdgeInsets.all(15.0),
              child: Text(
                'Leaderboard',
                style: TextStyle(color: Colors.black, fontSize: 22.0),
              ),
            ),
            Divider(color: Colors.redAccent),
            Row(crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[
              Padding(
                padding: EdgeInsets.all(15.0),
                child: CircleAvatar(
                    radius: 30.0, backgroundImage: ExactAssetImage('assets/cat.jpg')),
              ),
              Text('Neha'),
              RaisedButton(
                padding: EdgeInsets.symmetric(horizontal: 30.0, vertical: 15.0),
                textColor: Colors.black,
                child: Text(
                  '546',
                  style: TextStyle(),
                ),
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10.0)),
              ),
            ])
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Leaderboard Demo')),
      body: Center(
        // Use the method to get the latest Dialog instance when showing it
        child: ElevatedButton(
          onPressed: () {
            showDialog(
              context: context,
              builder: (context) => _buildLeadDialog(),
            );
          },
          child: Text('Show Leaderboard'),
        ),
      ),
    );
  }
}

Why This Works

By wrapping your Dialog in a method, every time you trigger Hot Reload, Flutter will re-run the build() method, which in turn calls _buildLeadDialog() to create a brand-new Dialog instance with your updated code. This ensures your changes are reflected immediately without needing a full app restart.

Quick Note on Hot Reload

Remember: Hot Reload doesn't reset global variables, static fields, or already-initialized objects. Any widget or UI element you want to update with Hot Reload should be created dynamically within the build() method or a helper function that's called during the build process.

内容的提问来源于stack exchange,提问作者Prianca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:32:58