【问题标题】:Flutter APK file is showing only gray screenFlutter APK文件只显示灰屏
【发布时间】:2022-01-02 00:55:51
【问题描述】:

我的 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 flutter apk bundle


【解决方案1】:

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

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

  • 找到 SdkManager(这取决于您的操作系统),然后您将运行如下内容: ./sdkmanager --install "cmdline-tools;latest"
  • 之后,您只需接受 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 question 上查看更多相关信息。

希望对您有所帮助?。

【讨论】:

  • 已完成 Android 工具链设置并重新创建了 apk 文件,但仍然无法正常工作。现在 Flutter 医生说没有发现问题。
  • 我刚刚删除了 SystemChrome 的一部分进行测试,但是没有用。
猜你喜欢
  • 2021-07-15
  • 2021-09-30
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 2011-07-19
  • 2020-07-11
  • 1970-01-01
相关资源
最近更新 更多