【发布时间】:2018-10-19 16:09:32
【问题描述】:
我正在试用 Flutter,我的应用在模拟器和真实设备上的响应都非常缓慢。我收到这样的警告
跳过了 51 帧!应用程序可能在其上做太多工作 主线程。
我知道 Dart 是一种单线程编程语言,在 Android 中,我曾经使用用于异步的旧 new Thread(); 块来解决这个问题。我正在尝试在 Flutter 中应用相同的内容,并且我阅读了 Future await async 等等,但是当您从 Internet 读取数据时,这些示例似乎正在解决。在这个阶段,我的应用程序没有在线阅读任何内容,当我的屏幕有进度对话框时,当我打开一个新的屏幕/页面时,以及应用程序上的每个动画时,我都会跳过 x 帧。这是我遇到问题的课程示例:
_buildContent(){
return new Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new InkWell(
onTap: (){
Navigator.push(context, new MaterialPageRoute(builder:
(context) => new LoginScreen()));
},
child: new Container(
height: 150.0,
width: 150.0,
child: new Image.asset("images/logo.png", fit: BoxFit.cover,),
),
),
new Container(
margin: const EdgeInsets.all(16.0),
child: new CircularProgressIndicator(
value: null,
strokeWidth: 1.0,
valueColor: new AlwaysStoppedAnimation<Color>(
Colors.white
)
),
)
],
);
}
我假设跳过的 x 帧警告是由进度对话框引起的?我有另一个屏幕(登录屏幕),它在打开时将小部件设置为动画,动画移动得非常慢,我可以从字面上看到正在渲染的每一帧。是否有在线教程可以帮助解决这个问题或只是理解 Dart 的异步编程?
【问题讨论】:
-
性能应该在发布版本
flutter build apk --release中检查。众所周知,动画小部件中的较大图像会导致性能问题。 -
Future不是一个不同的线程 -
@spongyboxx 此对话框不应导致性能问题。你很可能在其他地方有一些花费你很多的东西。尝试使用
Observatory来追踪花费这么多的东西。 -
请记住,期货不在特定线程上。它们只是稍后在主线程上执行。因此,如果您在
Future中进行昂贵的计算,它会冻结您的应用 -
Gunter 和 Remi 都提到过,
Futureis not a thread。