为什么我的Flutter应用在调试模式下运行正常,但在发布后不起作用。

huangapple go评论72阅读模式
英文:

Why my flutter app works in debug mode but not after release

问题

I have read some other topics in stackovwrflow with similar problems, But none is answered. So I'm asking here.
我已经阅读了一些在Stack Overflow上与类似问题相关的帖子,但没有得到答案。所以我在这里提问。

I am developing an airhockey game using flutter in android studio. Everything is normal in debbuging mode. But when I build a release version, it's not working correctly.
我正在使用Flutter在Android Studio中开发一款空中曲棍球游戏。在调试模式下一切正常。但是当我构建发布版本时,它不能正常工作。

Maybe this informations help:
也许这些信息会有所帮助:

I tested the app on a Xiaomi which uses android 4. In that device, sometimes the app will open, but most of the time only a black screen appears and nothing else. (about 70% of the time it fails.)
我在一台使用Android 4的小米设备上测试了这个应用程序。在该设备上,有时应用程序会打开,但大多数情况下只会出现黑屏,没有其他内容。(大约70%的时间会失败。)

An other device is a Samsung galaxy J7 with android 8. In that case, the black screen happens more. (about 90%).
另一台设备是三星Galaxy J7,使用Android 8。在这种情况下,黑屏的问题更加严重。(大约90%的时间会出现黑屏。)

The third device is a Xiaomi POCO X3 Pro with android 11. In that case, I could not use the app for just a single time.
第三台设备是小米POCO X3 Pro,使用Android 11。在这种情况下,我一次都无法使用该应用程序。

The libraries I used are:
我使用的库包括:

'package:flutter/services.dart'
'dart:async'
'dart:math'

Let me know if you need more information.
如果您需要更多信息,请告诉我。
By the way, you can get code from github to look in case you need.
顺便说一下,您可以从GitHub获取代码,以便查看,如果需要的话。

英文:

I have read some other topics in stackovwrflow with similar problems, But none is answered. So I'm asking here.
I am developing an airhockey game using flutter in android studio. Everything is normal in debbuging mode. But when I build a release version, it's not working correctly.

Maybe this informations help:

I tested the app on a Xiaomi which uses android 4. In that device, sometimes the app will open. but most of the time only a black screen appears and nothing else. (about 70% of the time it fails.)
An other device is a Samsung galaxy J7 with android 8. In that case, the black screen happens more. (about 90%).
The third device is a Xiaomi POCO X3 Pro with android 11. In that case, I could not use the app for just a single time.

The libraries I used are:

'package:flutter/services.dart'
'dart:async'
'dart:math'

let me know if you need more information.
bye the way you can get code from github to look in case you need.

答案1

得分: 1

**修复后**

如果您的 `environment sdk: '>=2.x <3.0.0'`,则可以使用以下代码:

```dart
import 'dart:ui';

Future<void> main() async {
  if (window.physicalSize.isEmpty) {
    window.onMetricsChanged = () {
      if (!window.physicalSize.isEmpty) {
        window.onMetricsChanged = null;
        runApp(const MyApp());
      }
    };
  } else {
    runApp(const MyApp());
  }
}

如果您的 environment sdk: ^3.0.0,可以使用以下代码:

Future<void> main() async {
  FlutterView? flutterView = PlatformDispatcher.instance.views.firstOrNull;
  if (flutterView == null || flutterView.physicalSize.isEmpty) {
    PlatformDispatcher.instance.onMetricsChanged = () {
      flutterView = PlatformDispatcher.instance.views.firstOrNull;
      if (flutterView != null && !flutterView!.physicalSize.isEmpty) {
        PlatformDispatcher.instance.onMetricsChanged = null;
        runApp(const MyApp());
      }
    };
  } else {
    runApp(const MyApp());
  }
}
英文:

Before Fixing

void main() {
  runApp(const MyApp());
}

Please change it to the Following:

If your environment sdk: &#39;&gt;=2.x &lt;3.0.0&#39; you can use the following code:

import &#39;dart:ui&#39;;

Future&lt;void&gt; main() async {
  if (window.physicalSize.isEmpty) {
    window.onMetricsChanged = () {
      if (!window.physicalSize.isEmpty) {
        window.onMetricsChanged = null;
        runApp(const MyApp());
      }
    };
  } else {
    runApp(const MyApp());
  }
}

If your environment sdk: ^3.0.0 can use the following code:

Future&lt;void&gt; main() async {
  FlutterView? flutterView = PlatformDispatcher.instance.views.firstOrNull;
  if (flutterView == null || flutterView.physicalSize.isEmpty) {
    PlatformDispatcher.instance.onMetricsChanged = () {
      flutterView = PlatformDispatcher.instance.views.firstOrNull;
      if (flutterView != null &amp;&amp; !flutterView!.physicalSize.isEmpty) {
        PlatformDispatcher.instance.onMetricsChanged = null;
        runApp(const MyApp());
      }
    };
  } else {
    runApp(const MyApp());
  }
}

huangapple
  • 本文由 发表于 2023年5月29日 12:50:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定