我开发的应用程序是在 Windows 上运行并在 flutter 中制作的:它已经投入生产一年多了,并且在不同客户的大约一百台不同计算机上运行,一切都运行良好。
但是有一台计算机,没有突出的细节将其与其他计算机分开,我的应用程序在这台计算机上根本无法工作:它加载了,但 GUI 仅显示灰色背景,如果它在 IDE 中运行,则会出现灰色背景。替换为红色背景,并用黄色文本详细说明异常情况。
尽管如此,我还是设法获得了异常,并且它包含的信息非常少,而且令我遗憾的是,该异常是在谷歌上寻找的最糟糕的异常:
Null check operator used on a null value
#0 Directionality.of (package:flutter/src/widgets/basic.dart:178)
#1 RichText.createRenderObject (package:flutter/src/widgets/basic.dart:5881)
#2 RenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6586)
#3 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:7042)
#4 Element.inflateWidget (package:flutter/src/widgets/framework.dart:4468)
#5 Element.updateChild (package:flutter/src/widgets/framework.dart:3963)
#6 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5642)
#7 Element.rebuild (package:flutter/src/widgets/framework.dart:5333)
#8 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:5599)
#9 ComponentElement.mount (package:flutter/src/widgets/framework.dart:5593)
#10 Element.inflateWidget (package:flutter/src/widgets/framework.dart:4468)
#11 Element.updateChild (package:flutter/src/widgets/framework.dart:3957)
#12 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6907)
#13 Element.updateChild (package:flutter/src/widgets/framework.dart:3941)
#14 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5642)
#15 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5780)
#16 Element.rebuild (package:flutter/src/widgets/framework.dart:5333)
#17 BuildScope._tryRebuild (package:flutter/src/widgets/framework.dart:2693)
#18 BuildScope._flushDirtyElements (package:flutter/src/widgets/framework.dart:2752)
#19 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:3048)
#20 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:1162)
#21 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:468)
#22 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1397)
#23 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1318)
#24 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1176)
#28 _invoke (dart:ui/hooks.dart:314)
#29 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:419)
#30 _drawFrame (dart:ui/hooks.dart:283)
(elided 3 frames from dart:async)
正如我所说,计算机看起来很正常:Windows 11 版本 24H2 build 26100.2033,搭载 Radeon Graphics 的 AMD Ryzen 5 6600H,没有安装特殊语言或似乎与顶部出现的
Directionality
小部件链接的内容堆栈跟踪的。为了明确起见,我会重复一遍:我的代码中没有缺少
!
或任何通常会导致 Null check operator
异常的内容。堆栈跟踪指向原始的 flutter 代码,而不是我的。
这很难知道,但这里有一些要点:
我将从这里开始。 在这一行:#1 RichText.createRenderObject 我们有这个方法
@override
RenderParagraph createRenderObject(BuildContext context) {
assert(textDirection != null || debugCheckHasDirectionality(context));
return RenderParagraph(text,
textAlign: textAlign,
textDirection: textDirection ?? Directionality.of(context),
softWrap: softWrap,
overflow: overflow,
textScaler: textScaler,
maxLines: maxLines,
strutStyle: strutStyle,
textWidthBasis: textWidthBasis,
textHeightBehavior: textHeightBehavior,
locale: locale ?? Localizations.maybeLocaleOf(context),
registrar: selectionRegistrar,
selectionColor: selectionColor,
);
}
这里#0 Directionality.of:
static TextDirection of(BuildContext context) {
assert(debugCheckHasDirectionality(context));
final Directionality widget = context.dependOnInheritedWidgetOfExactType<Directionality>()!;
return widget.textDirection;
}
这里: https://api.flutter.dev/flutter/widgets/debugCheckHasDirectionality.html
您有权访问这台计算机,请尝试在 debugCheckHasDirectionality 中进行跟踪。