Flutter APK文件仅显示灰色屏幕



我的APK文件只显示灰色屏幕,但它在调试模式下运行良好。

Flutter Doctor(编辑):

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.8.0, on Microsoft Windows [Version 10.0.19043.1415], locale ko-KR)
[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[√] Chrome - develop for the web
[√] Android Studio (version 2020.3)
[√] VS Code (version 1.63.2)
[√] Connected device (3 available)
• No issues found!

main.dart:

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await Hive.initFlutter();
await Hive.openBox("myBox1");
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]
).then((_) => runApp(const MyApp()));
}

AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET"/>

是什么导致了这个问题?

您是否已经尝试完成Android Toochain设置?我的意思是,它说你没有cmdline-tools component,你也没有接受android许可证。

它说你已经安装了Android Studio,所以你唯一要做的就是:

  • 找到SdkManager(这取决于你的O.S.),然后你会运行这样的程序:CCD_ 2
  • 之后,您只需接受Android许可证,如下所示:flutter doctor --android-licenses(注意,我建议在上一步之后重新启动控制台,然后尝试此命令)

注意:我想把它添加到评论部分,尽管我没有所需的声誉。

如果这不能解决问题,我想这可能是因为SystemChrome的东西。

所以我建议测试它,而不是这样:

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await Hive.initFlutter();
await Hive.openBox("myBox1");
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]
).then((_) => runApp(const MyApp()));
}

有了这个:


import 'package:flutter/foundation.dart' show kIsWeb;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await Hive.initFlutter();
await Hive.openBox("myBox1");
if (kIsWeb) {
// running on the web!
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]
).then((_) => runApp(const MyApp()));
} else {
// NOT running on the web! You can check for additional platforms here.
runApp(const MyApp()));
}

}

有一个全局布尔值kIsWeb,它可以告诉你该应用程序是否编译为在网络上运行。

文件:https://api.flutter.dev/flutter/foundation/kIsWeb-constant.html

您可以在这个StackOverflow问题上查看更多信息。

我希望这会有所帮助。

最新更新