警告:空感知操作的操作数 '!'具有类型 'WidgetsBinding' ,不包括 null

Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null

当我进行 flutter 升级时 运行 我的应用程序发生了这个错误。

../../../development/tools/flutter/.pub-cache/hosted/pub.dartlang.org/responsive_sizer-3.0.6+1/lib/src/helper.dart:56:33:警告:空感知操作的操作数 '!'具有类型 'WidgetsBinding' 排除 null.

而且该应用程序正在向我发出警告,但仍然 运行 正常 Error here

这是警告,不是错误。在 Flutter 3 中,WidgetsBindingSchedulerBinding 等绑定上的 instance 属性 现在是 non-nullable,因此使用 null-aware 运算符 ? 或空断言操作 ! 将导致此警告。

如果此警告像您的情况一样源自外部包,您可以联系开发人员并提出问题。尽管对于您的特定软件包,它应该已经在版本 3.0.7 或更高版本中解决,如所讨论的 here。所以升级包应该可以解决问题。

就您自己的代码而言,您可以 运行 dart fix --apply 并删除任何 null-aware 或空断言运算符。例如变化

SchedulerBinding.instance!.addPostFrameCallback(...);

SchedulerBinding.instance.addPostFrameCallback(...);

来自 Flutter 文档的 page 更详细地描述了您的选择。