dart Flutter:警告:可识别空值的操作“!”的操作数具有排除空值的类型“WidgetsBinding”

svmlkihl  于 2023-04-27  发布在  Flutter
关注(0)|答案(6)|浏览(195)

最近,我的调试控制台显示了一些以前没有显示的东西。当我使用flutter_typeahead包时会发生这种情况。我不知道这是错误还是警告。下面是我的调试控制台:

Launching lib\main.dart on Chrome in debug mode...
: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../Documents/flutter/packages/flutter/lib/src/widgets/binding.dart').
    WidgetsBinding.instance!.removeObserver(this);
                   ^

: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../Documents/flutter/packages/flutter/lib/src/widgets/binding.dart').
    WidgetsBinding.instance!.addObserver(this);
                   ^
: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../Documents/flutter/packages/flutter/lib/src/widgets/binding.dart').
    WidgetsBinding.instance!.addPostFrameCallback((duration) {
                   ^
: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../Documents/flutter/packages/flutter/lib/src/widgets/binding.dart').
    WidgetsBinding.instance!.removeObserver(this);
                   ^

: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../Documents/flutter/packages/flutter/lib/src/widgets/binding.dart').
    WidgetsBinding.instance!.addObserver(this);
                   ^

: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../Documents/flutter/packages/flutter/lib/src/widgets/binding.dart').
    WidgetsBinding.instance!.addPostFrameCallback((duration) {
                   ^
This app is linked to the debug service: ws://127.0.0.1:60355/d-7F-zZtGz0%3D/ws
Debug service listening on ws://127.0.0.1:60355/d-7F-zZtGz0=/ws
 Running with sound null safety
Connecting to VM Service at ws://127.0.0.1:60355/d-7F-zZtGz0=/ws

如果有人能提供建议,不胜感激。提前感谢!

h43kikqp

h43kikqp1#

如果您正在升级到较新版本,可能会弹出此类警告,因为它们与较新版本不兼容。您可以忽略它。此警告将消失!

pod7payv

pod7payv2#

这应该是一个非致命的警告。我忽略了它。

qyswt5oh

qyswt5oh3#

在工作室将Flutter更新到最新版本后,我也得到了同样的错误(我想我不小心在吐司中按错了按钮)。
在我的例子中,项目太大了,结果是更容易下载以前版本的Flutter(https://docs.flutter.dev/development/tools/sdk/releases),只需替换Flutter文件夹中的文件。

hfsqlsce

hfsqlsce4#

检查您拥有的Flutter版本并检查通道:

  • 要检查 Flutter 版本:flutter --version
  • 要检查 Flutter 通道:flutter channel
  • 要从主通道更改为稳定通道:flutter channel stable

然后做flutter upgrade。它可能会解决你的问题,它对我有效,希望这对你也有效。

ujv3wf0j

ujv3wf0j5#

我的flutter版本是3.0.0,我关注了这篇文章:https://docs.flutter.dev/development/tools/sdk/release-notes/release-notes-3.0.0来修复它们。
Ctrl +单击DEBUG CONSOLE中的flare_render_box.dart链接将其打开。在flare_render_box.dart文件中:

  1. Declare _ambiguate变量为全局变量,在所有导入flutter包的下面:
    T?_ambiguate(T?value)=〉value;
    1.替换“SchedulerBinding.instance?.cancelFrameCallbackWithId(_frameCallbackID);“与
    _ambiguate(SchedulerBinding.instance)!.cancelFrameCallbackWithId(_frameCallbackID);
    1.替换“SchedulerBinding.instance?.cancelFrameCallbackWithId(_frameCallbackID);“与
    _ambiguate(SchedulerBinding.instance)!.cancelFrameCallbackWithId(_frameCallbackID);
  2. Replace“_frameCallbackID = SchedulerBinding.instance?.scheduleFrameCallback(_beginFrame)??-1;“与
    _frameCallbackID = _ambiguate(SchedulerBinding.instance)!.scheduleFrameCallback(_beginFrame);
    1.然后保存它并重建您的应用程序。
6rqinv9w

6rqinv9w6#

这在我看来不像是一个错误。自从Dart升级到null-safety后,它会警告你这个包的一些类还没有升级到null-safety。我最近在我的项目中使用了这个包,但我没有收到这个警告。很可能你使用的是旧版本的包。我想让你做几件事。首先,转到pubspec.yaml文件并检查flutter_typeahaed的版本。如果它比版本3.2.4旧,请像下面这样更新它。

dependencies:
  flutter_typeahead: ^3.2.4

然后,在终端中分别键入以下命令。

flutter clean
flutter pub get

问题最有可能出现在flutter_typeahed包版本中。

相关问题