4

我注意到当我扩展 MaterialPageRoutes 然后像这样实例化它们时

class Route extends MaterialPageRoute {
  Route(): super(
    builder: (context) {
      return SomeWidget();
    }
  );
}
...somewhere in the build method of another class...
Navigator.push(context, Route())

热重载不会像我那样重建路线的内容

...somewhere in the build method of another class...
Navigator.push(context, MaterialPageRoute(
  builder: (context) {
    return SomeWidget();
  }
))

或者

class Route extends MaterialPageRoute {
  Route(): super(
    builder: (context) {
      return RouteWidget();
    }
  );
}
class RouteWidget extends StatelessWidget {
  Widget build(BuildContext context) {
    return SomeWidget();
  }
}
...somewhere in the build method of another class...
Navigator.push(context, Route())

我发现最后的解决方法特别奇怪。知道我错过了什么吗?文档说热重载将重建所有小部件。

4

1 回答 1

0

热重载应用代码更改并重新运行build(),以便更新视图。要重新加载路由,需要热重启。

有关热重载的更多详细信息,请参阅:https ://flutter.io/docs/development/tools/hot-reload

于 2021-11-25T04:11:12.070 回答