Flutter:警告:空感知操作的操作数'!'具有 'WidgetsBinding' 类型,不包括 null。升级到 flutter 3.0.0 时
Flutter: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null. when upgrading to flutter 3.0.0
升级到flutter 3.0.0后,运行应用开始出现问题,
(该应用程序可以运行,但终端中有 error/s(warning/s))。
这似乎是一个绑定问题。
错误(警告)说:
警告:空感知操作的操作数 '!'具有类型 'WidgetsBinding' 排除 null.
: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
../…/src/keyboard_visibility.dart:21
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../fvm/versions/3.0.0/packages/flutter/lib/src/widgets/binding.dart').
package:flutter/…/widgets/binding.dart:1
WidgetsBinding.instance!.addObserver(this);
^
: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
../…/src/keyboard_visibility.dart:37
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../fvm/versions/3.0.0/packages/flutter/lib/src/widgets/binding.dart').
package:flutter/…/widgets/binding.dart:1
WidgetsBinding.instance!.removeObserver(this);
^
: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
../…/src/bot_toast_init.dart:15
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../fvm/versions/3.0.0/packages/flutter/lib/src/widgets/binding.dart').
package:flutter/…/widgets/binding.dart:1
WidgetsBinding.instance!.addObserver(this);
^
: Warning: Operand of null-aware operation '!' has type 'SchedulerBinding' which excludes null.
../…/src/bot_toast_manager.dart:6
- 'SchedulerBinding' is from 'package:flutter/src/scheduler/binding.dart' ('../../../fvm/versions/3.0.0/packages/flutter/lib/src/scheduler/binding.dart').
package:flutter/…/scheduler/binding.dart:1
SchedulerBinding.instance!.addPostFrameCallback((_) {
^
: Warning: Operand of null-aware operation '!' has type 'SchedulerBinding' which excludes null.
../…/src/bot_toast_manager.dart:9
- 'SchedulerBinding' is from 'package:flutter/src/scheduler/binding.dart' ('../../../fvm/versions/3.0.0/packages/flutter/lib/src/scheduler/binding.dart').
package:flutter/…/scheduler/binding.dart:1
SchedulerBinding.instance!.ensureVisualUpdate();
编辑:
- 问题与flutter 3.0.0部分包的兼容性有关
- (flutter 3 中有一些变化,所以现在包的作者应该赶上这些变化)。
f you see warnings about bindings
When migrating to Flutter 3, you might see warnings like the following:
Warning: Operand of null-aware operation '!' has type >'SchedulerBinding' which excludes null.
-例如喜欢套餐bot_toast
此软件包的 GitHub 存储库中存在未解决的问题
https://github.com/MMMzq/bot_toast/issues/133
所以问题应该会在发布更新后尽快解决,就像他们说的那样。
我们现在可以DISCARD THE WARNING等待这些包的作者处理flutter 3的新变化来解决问题并更新他们的包。
这些软件包的开发人员现在正在赶上这些更改,并且应该很快就会有受影响的软件包的新版本。
存在此问题的软件包示例:
These warnings should not be breaking anything (just causing log
spam, for which we apologize; we plan to fix that in the next release,
we didn't realize how big of an issue it would be). If your app no
longer works, that is unlikely to be related to this issue
参考:
https://github.com/flutter/flutter/issues/103561#issuecomment-1126416045
如果您使用的是Flutter v3,需要升级一些依赖!!或者等待作者更新它们以与 Flutter v3 兼容。我通过降级解决了这个问题,现在我只使用以前的版本,因为我的项目处于相当先进的阶段。如果您认为降级最适合您,请查看此内容。
问题是WidgetsBinding.instance
return一个WidgetsBinding?在 flutter 版本 2.x.x 和较新的版本中,如 v3.0.x returns WidgetsBinding.
解决方案
如果警告来自您的代码而不是依赖项,支持 flutter 版本 2.9.x 和 3.0.x,并删除警告,您可以这样做
/// This allows a value of type T or T?
/// to be treated as a value of type T?.
T? _ambiguate<T>(T? value) => value;
_ambiguate(WidgetsBinding.instance)!.addObserver(this);
如果警告来自依赖项,您应该升级它或等待升级。
现在,您可以忽略它,将下面添加到调试控制台的过滤器中:
!WidgetsBinding, !^
升级到flutter 3.0.0后,运行应用开始出现问题,
(该应用程序可以运行,但终端中有 error/s(warning/s))。
这似乎是一个绑定问题。
错误(警告)说: 警告:空感知操作的操作数 '!'具有类型 'WidgetsBinding' 排除 null.
: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
../…/src/keyboard_visibility.dart:21
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../fvm/versions/3.0.0/packages/flutter/lib/src/widgets/binding.dart').
package:flutter/…/widgets/binding.dart:1
WidgetsBinding.instance!.addObserver(this);
^
: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
../…/src/keyboard_visibility.dart:37
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../fvm/versions/3.0.0/packages/flutter/lib/src/widgets/binding.dart').
package:flutter/…/widgets/binding.dart:1
WidgetsBinding.instance!.removeObserver(this);
^
: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
../…/src/bot_toast_init.dart:15
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../fvm/versions/3.0.0/packages/flutter/lib/src/widgets/binding.dart').
package:flutter/…/widgets/binding.dart:1
WidgetsBinding.instance!.addObserver(this);
^
: Warning: Operand of null-aware operation '!' has type 'SchedulerBinding' which excludes null.
../…/src/bot_toast_manager.dart:6
- 'SchedulerBinding' is from 'package:flutter/src/scheduler/binding.dart' ('../../../fvm/versions/3.0.0/packages/flutter/lib/src/scheduler/binding.dart').
package:flutter/…/scheduler/binding.dart:1
SchedulerBinding.instance!.addPostFrameCallback((_) {
^
: Warning: Operand of null-aware operation '!' has type 'SchedulerBinding' which excludes null.
../…/src/bot_toast_manager.dart:9
- 'SchedulerBinding' is from 'package:flutter/src/scheduler/binding.dart' ('../../../fvm/versions/3.0.0/packages/flutter/lib/src/scheduler/binding.dart').
package:flutter/…/scheduler/binding.dart:1
SchedulerBinding.instance!.ensureVisualUpdate();
编辑: - 问题与flutter 3.0.0部分包的兼容性有关
- (flutter 3 中有一些变化,所以现在包的作者应该赶上这些变化)。
f you see warnings about bindings When migrating to Flutter 3, you might see warnings like the following:
Warning: Operand of null-aware operation '!' has type >'SchedulerBinding' which excludes null.
-例如喜欢套餐bot_toast 此软件包的 GitHub 存储库中存在未解决的问题 https://github.com/MMMzq/bot_toast/issues/133 所以问题应该会在发布更新后尽快解决,就像他们说的那样。
我们现在可以DISCARD THE WARNING等待这些包的作者处理flutter 3的新变化来解决问题并更新他们的包。 这些软件包的开发人员现在正在赶上这些更改,并且应该很快就会有受影响的软件包的新版本。
存在此问题的软件包示例:
These warnings should not be breaking anything (just causing log spam, for which we apologize; we plan to fix that in the next release, we didn't realize how big of an issue it would be). If your app no longer works, that is unlikely to be related to this issue
参考:
https://github.com/flutter/flutter/issues/103561#issuecomment-1126416045
如果您使用的是Flutter v3,需要升级一些依赖!!或者等待作者更新它们以与 Flutter v3 兼容。我通过降级解决了这个问题,现在我只使用以前的版本,因为我的项目处于相当先进的阶段。如果您认为降级最适合您,请查看此内容。
问题是WidgetsBinding.instance
return一个WidgetsBinding?在 flutter 版本 2.x.x 和较新的版本中,如 v3.0.x returns WidgetsBinding.
解决方案
如果警告来自您的代码而不是依赖项,支持 flutter 版本 2.9.x 和 3.0.x,并删除警告,您可以这样做
/// This allows a value of type T or T?
/// to be treated as a value of type T?.
T? _ambiguate<T>(T? value) => value;
_ambiguate(WidgetsBinding.instance)!.addObserver(this);
如果警告来自依赖项,您应该升级它或等待升级。
现在,您可以忽略它,将下面添加到调试控制台的过滤器中:
!WidgetsBinding, !^