Flutter 热重载在 android studio(mac) 中不起作用 [英] Flutter Hot reloading not working in android studio(mac)

查看:37
本文介绍了Flutter 热重载在 android studio(mac) 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 mac 机器上工作时,我发现热重载在 Android Studio 中不起作用.我不知道怎么了 :)

While working on mac machine I found that hot reloading not working in Android Studio. I don't know what's wrong :)

这是不重新加载的代码(我正在将 Hello world 文本更改为 Flutter Sample 文本)

Here is the code which not reloading(I am changing the Hello world text to Flutter Sample text)

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: new Scaffold(
      appBar: new AppBar(
        title: new Text("Demo Project"),
      ),
      body: Center(child: new Text("Hello World!!!")),
    ),
  ));
}

请看下面带有链接的视频https://www.dropbox.com/s/e7viujcgv8w0mtr/hot_reload.mp4?dl=0

Please see this below video with the link https://www.dropbox.com/s/e7viujcgv8w0mtr/hot_reload.mp4?dl=0

这不会阻止我的发展,但我担心它为什么会发生.

This not stop my development, But I am concerned why it is happening.

推荐答案

Flutter 文档说热重载将代码更改加载到 VM 中并重新构建小部件树,保留应用程序状态;它不会重新运行 ma​​in() 或 initState()"

Flutter Documentation says "Hot reload loads code changes into the VM and re-builds the widget tree, preserving the app state; it doesn’t rerun main() or initState()"

因此,看起来如果应用逻辑位于 ma​​in 方法 之外,那么热重载就可以工作.尝试像下面这样重构您的应用,然后执行重启(绿色按钮).您所做的任何后续更改都将在保存时热重载.

So, it looks like if the app logic resides outside the main method, then Hot Reloading works. Try to refactor your app like below and then do Restart (green button). Any subsequent changes you make, will be HOT RELOADED when saved.

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text("Demo Project"),
        ),
        body: Center(child: new Text("Hello World!!!")),
      ),
    );
  }
}

更新:如果您要添加新资产(如图像或媒体文件),则热重载将不起作用.在这些情况下,您必须重新启动应用程序.

UPDATE: Hot Reload will not work if you are adding new assets (like images, or medial files). In those cases you will have to restart the App.

这篇关于Flutter 热重载在 android studio(mac) 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆