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

Viewed 13378

Recently, my Debug Console was showing something that wasn't showing before. This happens when I use package flutter_typeahead. I don't know if this is an error or a warning. Below is my Debug Console:

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

Appreciate if someone can advise. Thank you in advance!

6 Answers

If you are upgrading to a newer version, such warnings may pop up because they are not compatible with the newer version. You can ignore it. This warning will disappear!

This should be a non-fatal warning. I ignored it.

check which version of flutter u have and check the channel

to check flutter version: flutter --version

to check flutter channel: flutter channel

to change from master to stable channel: flutter channel stable

then do flutter upgrade.

it might solve your problem, it worked for me ,hope this works for you too

I got the same error after the Studio updated Flutter to the latest version (I think I accidentally hit the wrong button in the toast).

In my case, the project is too big, it turned out to be easier to download the previous version of Flutter (https://docs.flutter.dev/development/tools/sdk/releases) and just replace the files in the Flutter folder.

Some changes work for me. My flutter version is 3.0.0 and I followed this post: https://docs.flutter.dev/development/tools/sdk/release-notes/release-notes-3.0.0 to fix them.

Ctrl + click on flare_render_box.dart link in your DEBUG CONSOLE to open it. In flare_render_box.dart file:

  1. Declare _ambiguate variable as global, right in below all import flutter package:

T? _ambiguate(T? value) => value;

  1. Replace "SchedulerBinding.instance?.cancelFrameCallbackWithId(_frameCallbackID);" with

_ambiguate(SchedulerBinding.instance)!.cancelFrameCallbackWithId(_frameCallbackID);

  1. Replace "SchedulerBinding.instance?.cancelFrameCallbackWithId(_frameCallbackID);" with

_ambiguate(SchedulerBinding.instance)!.cancelFrameCallbackWithId(_frameCallbackID);

  1. Replace "_frameCallbackID = SchedulerBinding.instance?.scheduleFrameCallback(_beginFrame) ?? -1;" with

_frameCallbackID = _ambiguate(SchedulerBinding.instance)!.scheduleFrameCallback(_beginFrame);

  1. Then save it and rebuild your app.

This doesn't seem to me like an error. Since Dart upgraded to null-safety it warns you about some classes for this package that haven't been upgraded to null safety. I have recently used this package in my project but I didn't get this warning. Most likely you're using the older version of the package. I want you to do a couple of things. First, go to pubspec.yaml file and check the version of flutter_typeahaed. If it's older than version 3.2.4 please update it like the following.

dependencies:
  flutter_typeahead: ^3.2.4

Then, type into the terminal the following commands respectively.

flutter clean
flutter pub get

The problem is most likely in the flutter_typeahed package version.

Related