使用命令行
flutter run --release --verbose
可以在模拟器上,模拟release上的运行状态,关掉app进程,再打开也有日志
白屏原因:
Flutter为加快应用启动速度,未等size赋值,就开始渲染界面。
解决办法:
监听window的尺寸变化,当不为空时,再runApp;runApp必须先WidgetsFlutterBinding.ensureInitialized(),确保有WidgetsFlutterBinding实例,不然会黑屏
Future<void> main() async {
//如果size是0,则设置回调,在回调中runApp
if(window.physicalSize.isEmpty){
print("window size is zero");
window.onMetricsChanged = (){
//在回调中,size仍然有可能是0
if(!window.physicalSize.isEmpty){
window.onMetricsChanged = null;
print("window onMetricsChanged,run app");
runMyApp();
}
};
} else{
//如果size非0,则直接runApp
print("window load success,run app");
runMyApp();
}
}
void runMyApp() async{
print("window: ${window.physicalSize.width} ${window.physicalSize.height}");
//需确保加载完成,才runApp
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
使用window需导入 dart:ui
import 'dart:ui';