Flutter: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null. when upgrading to flutter 3.0.0

38.4k Views Asked by At

When upgraded to flutter 3.0.0 , there is a problem started to appear when running the app,

(The app works but there are error/s (warning/s) in the terminal).

It seems like a Binding issue.

the error(warning) says: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.

: 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();

Edit: - The problem is related to the compatibility of some packages with the flutter 3.0.0

  • (There are some changes in flutter 3, so now authors of the packages should be catching up for these changes).

Flutter 3.0.0 release notes

f you see warnings about bindings When migrating to Flutter 3, you might see warnings like the following:

Warning: Operand of null-aware operation '!' has type >'SchedulerBinding' which excludes null.

-for example like the package bot_toast There is an open issue on the GitHub repository of this package https://github.com/MMMzq/bot_toast/issues/133 so the problem should be fixed soon after releasing an update as they said.

4

There are 4 best solutions below

1
On BEST ANSWER

We can now DISCARD THE WARNING and wait until the authors of these packages deal with the new changes in flutter 3 to solve the problem and update their packages. The developers of the packages are now catching up on these changes and there should be new versions of the affected packages soon.

Example of a package with this issue:

These warnings should not be breaking anything (just causing log spam, for which we apologize; we plan to fix that in the next release, we didn't realize how big of an issue it would be). If your app no longer works, that is unlikely to be related to this issue

reference:

https://github.com/flutter/flutter/issues/103561#issuecomment-1126416045

0
On

The problem is that WidgetsBinding.instance return a WidgetsBinding? in flutter version 2.x.x and in newer version like v3.0.x returns WidgetsBinding.

SOLUTION

If warning is coming from your code and not from dependencies,to support both flutter version 2.9.x and 3.0.x,and remove the warning you can do

  /// 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);

If the warning comes from a dependencies you should upgrade it or otherwise wait until is upgraded.

0
On

We can simply ignore the error for now. We are seeing this error as a side effect of simplification of APIs. Refer to flutter 3.0 release notes for more info on this.

https://docs.flutter.dev/development/tools/sdk/release-notes/release-notes-3.0.0

0
On

First you have to clean yourproject

flutter clean
flutter pub get
flutter pub upgrade

Check the compileSdkVersion and Kotlin version. It should work now.