AssertionError: 在发布模式下 '!_debugAllowCreation' 不为真
AssertionError: '!_debugAllowCreation' is not true in release mode
ID: flutter/release-mode-debug-assert
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Flutter 3.13.0 | active | — | — | — |
| Flutter 3.22.0 | active | — | — | — |
| Dart 3.1.0 | active | — | — | — |
根因分析
一个仅限调试的断言检查小部件创建权限,在发布模式下被触发,因为本应禁用的代码路径(例如调试覆盖层或检查器)由于构建标志配置错误或遗留的调试代码仍然处于活动状态。
English
A debug-only assertion that checks widget creation permissions is triggered in release mode because the code path that should be disabled (e.g., a debug overlay or inspector) is still active due to a misconfigured build flag or leftover debug code.
官方文档
https://docs.flutter.dev/testing/debugging#debug-mode-assertions解决方案
-
确保调试特定代码(如 debugPaintSizeEnabled 或自定义调试覆盖层)包装在 'assert(() { ... }());' 中或使用 'kReleaseMode' 条件编译。示例: if (kDebugMode) { // 仅调试的小部件创建 } -
运行 'flutter clean' 并使用 'flutter build apk --release' 重新构建,确保没有残留的调试工件。
-
检查可能注入调试小部件的第三方包;更新它们或通过包配置排除调试功能。
无效尝试
常见但无效的做法:
-
Add '--debug' flag to the build command to avoid release mode
70% 失败
The error occurs in release mode; using debug mode bypasses the issue but doesn't solve the root cause for production builds.
-
Remove all assert() statements from the codebase
90% 失败
Assertions are stripped in release mode by default; the error is from Flutter's internal assertions, not user code.
-
Set '--no-sound-null-safety' to suppress the assertion
80% 失败
Null safety is unrelated; the assertion is about widget creation permissions, not null checks.