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

6

5

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!

Stet answered 28/2, 2022 at 0:53 Comment(2)
It should be a non-fatal warning. Either ignore it or temporarily switch from the master branch to the latest Flutter stable release until the problematic package is updated.Emend
Does this answer your question? Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null. when upgrading to flutter 3.0.0Bradybradycardia
J
9

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!

Jab answered 12/5, 2022 at 7:56 Comment(0)
S
7

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

Stet answered 9/3, 2022 at 4:11 Comment(0)
C
2

Check which version of Flutter you 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.

Clarhe answered 7/3, 2022 at 6:30 Comment(6)
I have followed your way but still have this warning.Stet
Flutter 2.10.3 • channel stable • github.com/flutter/flutter.git Framework • revision 7e9793dee1 (6 days ago) • 2022-03-02 11:23:12 -0600 Engine • revision bd539267b4 Tools • Dart 2.16.1 • DevTools 2.9.2 this my flutter version what's your's...?Clarhe
Flutter 2.11.0-0.0.pre.582 • channel master • github.com/flutter/flutter.git Framework • revision 9514106108 (3 weeks ago) • 2022-02-16 00:00:11 -0500 Engine • revision b09cf53269 Tools • Dart 2.17.0 (build 2.17.0-105.0.dev) • DevTools 2.10.0Stet
try with stable channelClarhe
I have followed your way but still have this warning.Stet
I don't think it affect much for normal execution, it's just a warning you can ignore it.Clarhe
A
2

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.

Auriculate answered 12/5, 2022 at 14:56 Comment(0)
C
1

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.
Carding answered 10/6, 2022 at 15:55 Comment(0)
U
-2

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.

Undine answered 28/2, 2022 at 1:23 Comment(2)
I have followed your way but still have this warning.Stet
Can you share the implementation of the TypeAhead widget?Undine

© 2022 - 2024 — McMap. All rights reserved.