扑动:警告:识别空值的操作“!”的操作数具有排除空值的类型“WidgetsBinding”,升级到flutter 3.0.0时

t40tm48m  于 2023-03-13  发布在  Flutter
关注(0)|答案(4)|浏览(195)

升级到flutter 3.0.0后,在运行应用程序时开始出现问题,
(The应用程序工作,但终端中出现错误(警告))。
这似乎是一个绑定问题。
错误(警告)显示:警告:可识别null的操作“!”的操作数具有排除null的类型“WidgetsBinding”。

: 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中有一些变化,所以现在包的作者应该会赶上这些变化)。

Flutter 3.0.0发布说明
如果您看到关于绑定的警告迁移到Flutter 3时,您可能会看到如下警告:
Warning: Operand of null-aware operation '!' has type >'SchedulerBinding' which excludes null.
-例如bot_toast软件包https://github.com/MMMzq/bot_toast/issues/133的GitHub存储库中有一个未解决的问题,因此应该在发布更新后尽快修复该问题。

rseugnpd

rseugnpd1#

我们现在可以放弃这个警告,等到这些包的作者处理flutter 3中的新变化后再去解决问题并更新他们的包。这些包的开发者们正在跟进这些变化,应该很快就会有新版本的受影响的包。
出现此问题的软件包示例:
这些警告不应该破坏任何东西(只是造成日志垃圾邮件,我们为此道歉;我们计划在下一个版本中修复这个问题,但我们没有意识到这个问题会有多大)。如果您的应用程序不再工作,则不太可能与此问题有关
参考:
https://github.com/flutter/flutter/issues/103561#issuecomment-1126416045

izkcnapc

izkcnapc2#

首先你得清理你的项目

flutter clean
flutter pub get
flutter pub upgrade

检查compileSdkVersion和Kotlin版本。现在应该可以工作了。

rnmwe5a2

rnmwe5a23#

我们现在可以简单地忽略这个错误。我们认为这个错误是API简化的副作用。参考flutter 3.0发行说明了解更多信息。
https://docs.flutter.dev/development/tools/sdk/release-notes/release-notes-3.0.0

jchrr9hc

jchrr9hc4#

问题是WidgetsBinding.instance在flutter版本2.x.x中返回一个WidgetsBinding?,而在更新的版本(如v3.0.x)中返回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);

如果警告来自依赖项,则应升级该依赖项,或者等待升级。

相关问题