24

After updating Flutter 3.0 below compile error occured. This error has no ref to my code. It refers to framework.

Launching lib/main.dart on Chrome in debug mode...
lib/main.dart:1
: Warning: Operand of null-aware operation '!' has type 'SchedulerBinding' which excludes null.
../…/src/framework.dart:275
- 'SchedulerBinding' is from 'package:flutter/src/scheduler/binding.dart' ('../snap/flutter/common/flutter/packages/flutter/lib/src/scheduler/binding.dart').
package:flutter/…/scheduler/binding.dart:1
    if (SchedulerBinding.instance!.schedulerPhase ==
isa
  • 398
  • 1
  • 3
  • 13
  • any luck? facing same issue – Andriy Antonov May 13 '22 at 09:45
  • 1
    i think that the package also needed to be update which some code using Scheduler or Widgetbinding that are put with ! and remove them. but it's quite a torture for other package which will never be updated and leaving it be – Arbiter Chil May 13 '22 at 10:58

4 Answers4

16

It's expected when you migrate to Flutter 3.0. The docs also mention about this:

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

These are caused by a simplification of the API (the instance property on bindings is now non-nullable), combined with an eager compiler that wants to report any case where redundant null-aware operators (such as ! and ?.) that are used when they’re not necessary.

Solutions:

  1. You can update your packages by running flutter pub upgrade.

  2. If the issue persist, (say, some of the packages are still giving you this error), you can either ignore it as this is just a warning or simply edit the source file and remove ? and/or !.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
  • If you're editing the source file, make sure you do a full app restart. – CopsOnRoad May 15 '22 at 08:40
  • 1
    Editing the source code of third party libraries is not advisable, as you need to assure to run that specific version with the tweaks, but that wouldn't be the case as soon as you update the library. Also, the source code is usually read only. It's way better to have a couple of warnings from the compiler than having to edit other's source code. – Ekiden May 23 '22 at 10:08
  • @Ekiden I never said you **must** edit the source code. I said there's a possibility of doing it. Also, changing the source code in this particular scenario won't do any harm, and source code is open for a reason. – CopsOnRoad May 24 '22 at 18:08
  • It's not working. – Abhi S Jul 14 '22 at 06:18
  • 1
    Thanks `flutter pub upgrade` worked for this warning for me. – Hugh Barnard Jul 16 '22 at 13:17
12

Note that it is a warning, and not an error: The app will compile and run just fine.

It basically means that in many places in the code (in 3rd party packages and I think also in some 1st party instances) there is a null check, which is now redundant. That is, before you'd use

SchedulerBinding.instance!.someField == ...

Because SchedulerBinding.instance had the type SchedulerBinding?, which may (technically) be null. Since in practice, the ! operator was used, it was already assumed to not be null, and with the new changes in the SDK, they changed the type so it actually can never be null. Now, you can simply say

SchedulerBinding.instance.someField == ...

If you still include the ! or ? operators, your IDE will warn you, that this is a redundant check, as the instance can never be null. Unfortunately, we'll be stuck with these warnings until the packages you use and Flutter itself solve this, but you can definitely ignore them, as a unnecessary null check will never break your code.

fravolt
  • 2,565
  • 1
  • 4
  • 19
1

I was facing the same issue after searching many blogs and articles.

Solution: Please update all the outdated packages to major latest version.

andrewJames
  • 19,570
  • 8
  • 19
  • 51
Indrajeet Singh
  • 62
  • 2
  • 10
0

i am not sure if i am right.. I got this error after connecting firebase and making some changes to gradle. so next day, after uninstalling flutter, android studio (including sdk) and reinstalling.. i am not getting this error but i still havent reconnected firebase as of now.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 30 '22 at 03:57