12

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

Flutter Dev
  • 488
  • 3
  • 18

2 Answers2

11

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.

hnnngwdlch
  • 2,761
  • 1
  • 13
  • 20
  • 1
    But the issue is coming from different packages. So, what should be done now? – Santo Shakil May 15 '22 at 04:58
  • 2
    You can file an issue for each and wait for the developer to update the respective package. For many packages the newest version already addresses the issue or the developer is actively working on it. In the meantime we have two options: (1) Ignore the warning or (2) downgrade to Flutter version 2.10.5 for now. – hnnngwdlch May 15 '22 at 11:53
  • You need to wait/pray for the package's developer to fix on it. I'm wondering when Google devs are going to learn to maintain backward compatibility!! They should not throw changes like that! Once "SchedulerBinding.instance" is nullable, it should remain nullable FOREVER.... or change it in a way of not breaking the existent code base! – Christian May 16 '22 at 22:43
  • How can I ignore this warning instead? My package needs to compile for Flutter 2 and 3 without warning. Please see: https://stackoverflow.com/questions/72522152/how-to-ignore-operand-of-null-aware-operation – Martin Braun Jun 06 '22 at 18:45
1

If you need to fix this issue in a package and still want to support Flutter 2 it's actually better to bypass this issue like so:

/// This allows a value of type T or T? to be treated as a value of type T?.
///
/// We use this so that APIs that have become non-nullable can still be used
/// with `!` and `?` on the stable branch.
// See https://github.com/flutter/flutter/issues/64830
T? _ambiguate<T>(T? value) => value;

_ambiguate(SchedulerBinding.instance)!.addPostFrameCallback((_) {
    // ...
}

However, as soon as you ditch Flutter 2 support for further development, you should apply the accepted solution.

Ref: https://github.com/flutter/packages/pull/546/files

Martin Braun
  • 10,906
  • 9
  • 64
  • 105