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

when i made flutter upgrade then run my app this error occurs.

../../../development/tools/flutter/.pub-cache/hosted/pub.dartlang.org/responsive_sizer-3.0.6+1/lib/src/helper.dart:56:33: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.

  • 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../development/tools/flutter/packages/flutter/lib/src/widgets/binding.dart'). pixelRatio = WidgetsBinding.instance!.window.devicePixelRatio;

and also the app is getting me warning but still running normally Error here



Solution 1:[1]

This is a warning not an error. In Flutter 3 the instance property on bindings such as WidgetsBinding and SchedulerBinding is now non-nullable, hence using the null-aware operator ? or the null assertion operation ! will lead to this warning.

If this warning originates from an external package as in your case, you could reach out to the developer and file an issue. Although for your specific package it should already be solved in version 3.0.7 or later as discussed here. So upgrading the package should solve the issue.

As far as your own code is concerned, you can run dart fix --apply and remove any null-aware or null assertion operators. E. g. change

SchedulerBinding.instance!.addPostFrameCallback(...);

to

SchedulerBinding.instance.addPostFrameCallback(...);

This page from the Flutter docs describes your options in greater detail.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hnnngwdlch