Flutter 应用在 Android Studio 下运行时死机
Posted
技术标签:
【中文标题】Flutter 应用在 Android Studio 下运行时死机【英文标题】:Flutter app freezes when running under Android Studio 【发布时间】:2020-09-07 23:11:46 【问题描述】:我的颤振应用程序有一些繁重的模型初始化。所以它是在异步函数中创建的。这个函数产生的未来被传递给一个小部件。模型加载时 - 我想显示进度指示器。问题是当我从 android Studio 启动我的应用程序时 - 进度指示器冻结并且应用程序从不显示任何其他内容!值得一提的是,有时它会起作用。但说实话很少见。
这可以在 android 模拟器和真实设备上重现。如果从设备本身启动,它永远不会冻结。如果从 Android Studio 以调试模式启动,它也永远不会冻结。
我已经创建了一个最小的复制样本:
import 'package:flutter/material.dart';
Future<int> getTheAnswer() async
// To emulate long running method
await Future.delayed(Duration(seconds: 10));
return 42;
void main()
WidgetsFlutterBinding.ensureInitialized();
var theAnswer = getTheAnswer();
// This is just to show that the future was indeed completed
theAnswer.then((value) => print("Future completed with $value"));
runApp(MyApp(theAnswer));
class MyApp extends StatelessWidget
final Future<int> _myFuture;
MyApp(this._myFuture);
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
body: Center(
child: FutureBuilder<int>(
future: _myFuture,
builder: (context, snapshot)
if (!snapshot.hasData)
print("No data");
return CircularProgressIndicator();
// This part is not executed
print("Has data");
return Text(
"The Answer to the Ultimate Question of Life, the Universe, and Everything is $snapshot.data");
,
))),
);
这个输出:
Launching lib\main.dart on SM G960F in debug mode...
Running Gradle task 'assembleDebug'...
√ Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app.apk... 5.4s
Waiting for SM G960F to report its views...
Debug service listening on ws://127.0.0.1:54422/RHSBDMQJZjA=/ws
Syncing files to device SM G960F...
I/flutter (12538): No data
I/flutter (12538): Future completed with 42
还有flutter doctor -v
:
[√] Flutter (Channel master, 1.19.0-2.0.pre.133, on Microsoft Windows [Version 10.0.19041.208], locale en-US)
• Flutter version 1.19.0-2.0.pre.133 at C:\Repos\flutter
• Framework revision e92afc16b6 (13 hours ago), 2020-05-21 05:29:01 +0900
• Engine revision 2d4e83921d
• Dart version 2.9.0 (build 2.9.0-9.0.dev 40f7a11d89)
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
• Android SDK at C:\Users\am580\AppData\Local\Android\sdk
• Platform android-29, build-tools 29.0.2
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
• All Android licenses accepted.
[√] Android Studio (version 3.5)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 42.1.1
• Dart plugin version 191.8593
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
[√] IntelliJ IDEA Community Edition (version 2019.3)
• IntelliJ at C:\Users\am580\AppData\Local\JetBrains\Toolbox\apps\IDEA-C\ch-0\193.6911.18
• Flutter plugin version 45.0.2
• Dart plugin version 193.6911.31
[√] Connected device (2 available)
• SM G960F • 21f9139c290c7ece • android-arm64 • Android 10 (API 29)
• Android SDK built for x86 • emulator-5554 • android-x86 • Android 10 (API 29) (emulator)
• No issues found!
【问题讨论】:
【参考方案1】:切换到稳定频道后问题消失:
flutter channel stable
但是它破坏了 android 编译。修复它:
rm android/settings.gradle
flutter create .
【讨论】:
以上是关于Flutter 应用在 Android Studio 下运行时死机的主要内容,如果未能解决你的问题,请参考以下文章
是否可以在flutter应用程序关闭时通过平台通道从android后台服务向flutter端发送消息
无法在 Android 12.0 中退出 Flutter 应用
在 Android 上使用 Flutter 显示在其他应用上