随便写了个demo
, 修改widget
的颜色, 开启hot reload
发现并没有什么改变. 只有hot restart
才有反应
import 'package:flutter/material.dart';
void main() {
runApp(build());
}
Widget build() {
return MaterialApp(
home: Scaffold(
body: Center(
child: ConstrainedBox(
constraints: BoxConstraints.tight(const Size(100, 100)),
child: Container(
color: Colors.yellow,
width: 30,
height: 30,
),
),
),
));
}
跟记忆中的好像有点不太一样
那热加载(hot reload)
和热重启(hot restart)
有什么区别呢?
·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().(command\ in Intellij and Android Studio,^F5 in VSCode)
·Hot restart loads code changes into the VM, and restarts the Flutter app, losing the app state. (↑command\ in IntelliJ and Android Studio,↑commandF5 in VSCode)
热加载(hot reload)
加载代码修改到虚拟机, 重新build
了widget
树, 保留了app
状态, 它不会重走main
方法和initState
方法(但会走build
方法)
热重启(hot restart)
加载代码修改到虚拟机, 重启了Flutter app
, 丢失了app
状态, 它会重走main
方法
那么回到我上面的demo, 我这个widget
是在main
里面加载的, 但是hot reload
并不会重走main
, 所以改变是不会生效的. 但当你的改变在build
方法里时, 就可以生效
网友评论