如何在Flutter App的代码中检测热重装? [英] How to detect hot reload inside the code of a Flutter App?

查看:122
本文介绍了如何在Flutter App的代码中检测热重装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个资产文件,在使用该资产文件之前,需要对其进行处理.此资产文件将被大量编辑,我希望每次进行编辑时都不必重新启动应用程序.

I have an asset file that need to be processed before it can be used. This asset file will be heavily edited and I would like to not to have to restart the application each time I make an edit.

我知道State类上存在reassemble方法.但是,这需要有一个伪造的小部件来覆盖此方法,并将其放置在应用程序中的某个位置以获取有关热重装的通知.

I'm aware of the existence of the reassemble method on the State class. However, this requires having a dummy widget that overrides this method and putting it inside the app somewhere to get notified about hot reload.

class WdHotReloadNotifier extends StatefulWidget
{
  final Function callback;
  WdHotReloadNotifier(this.callback);
  @override
  State<StatefulWidget> createState() => WdHotReloadNotifierState(this.callback);
}
class WdHotReloadNotifierState extends State<WdHotReloadNotifier>
{
  Function callback;
  WdHotReloadNotifierState(this.callback);
  @override
  void reassemble()
  {
    super.reassemble();
    callback();
  }
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

然后我可以像这样使用它:

Then I can use it like this:

WdHotReloadNotifier((){print("HOT REALOADED 1");}),
WdHotReloadNotifier((){print("HOT REALOADED 2");}),

但是,将它们添加到单个页面意味着只要页面在堆栈中就可以使用.并且将它们添加到多个页面意味着这些挂钩将执行一次以上.

However, adding these to a single page means that it will work as long as the page is in the stack. And adding them to multiple pages means the hooks will execute more than once.

是否有办法在全球范围内获得有关热装弹的通知?

Is there a way in flutter to get notified globally about a hot reload?

推荐答案

您想要的是在State子类上覆盖reassemble方法. 但是您可以将小部件放置在其他位置以更改行为.

Overriding the reassemble method on a State subclass is what you want. But you can position the widget to a different location to change the behavior.

考虑以下窗口小部件,该窗口小部件在热重载时调用回调,并且不执行其他任何操作:

Consider the following widget which calls a callback on hot-reload and does nothing else:

class ReassembleListener extends StatefulWidget {
  const ReassembleListener({Key key, this.onReassemble, this.child})
      : super(key: key);

  final VoidCallback onReassemble;
  final Widget child;

  @override
  _ReassembleListenerState createState() => _ReassembleListenerState();
}

class _ReassembleListenerState extends State<ReassembleListener> {
  @override
  void reassemble() {
    super.reassemble();
    if (widget.onReassemble != null) {
      widget.onReassemble();
    }
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}

您可以随意在任何位置插入该小部件.

You're free to insert that widget wherever you like.

在单个页面上显示:

MaterialApp(
  home: ReassembleListener(onReassemble: () => print("Foo"), child: Home()),
)

或通过包装整个应用程序来全局:

Or globally by wrapping the whole application:

ReassembleListener(
  onReassemble: () => print('foo'),
  child: MaterialApp(
    home: Home(),
  ),
)

这篇关于如何在Flutter App的代码中检测热重装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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