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

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

最近,我的调试控制台显示了以前没有显示的内容。当我使用 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

如有任何帮助,我将不胜感激。

在我看来这不像是一个错误。自从 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 软件包版本中。

检查你使用的是哪个版本的 flutter 并检查频道

检查 flutter 版本:flutter --version

检查 flutter 通道:flutter channel

从主频道转为稳定频道:flutter channel stable

然后 flutter upgrade.

它可能会解决您的问题, 它对我有用,希望对你也有用

这应该是一个 non-fatal 警告。我忽略了它。

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

Studio 将 Flutter 更新到最新版本后出现同样的错误(我想我不小心按错了 toast 中的按钮)。

我的项目太大了,原来下载以前版本的Flutter(https://docs.flutter.dev/development/tools/sdk/releases)然后替换Flutter文件夹里的文件就好了。