Flutter Error: Method 'addPostFrameCallback' cannot be called on 'SchedulerBinding?' because it is potentially null

Viewed 11207

Whenever I try to run my Flutter app I get this error, it was running and I don't know why now it's not. I've even reset to previous commit and it still won't run.

../../../development/flutter/.pub-cache/hosted/pub.dartlang.org/get-4.6.3/lib/get_navigation/src/extension_navigation.dart:357:33: Error: Method 'addPostFrameCallback' cannot be called on 'SchedulerBinding?' because it is potentially null.
 - 'SchedulerBinding' is from 'package:flutter/src/scheduler/binding.dart' ('../../../development/flutter/packages/flutter/lib/src/scheduler/binding.dart').
Try calling using ?. instead.
      SchedulerBinding.instance.addPostFrameCallback((_) {
                                ^^^^^^^^^^^^^^^^^^^^
../../../development/flutter/.pub-cache/hosted/pub.dartlang.org/get-4.6.3/lib/get_navigation/src/extension_navigation.dart:468:33: Error: Method 'addPostFrameCallback' cannot be called on 'SchedulerBinding?' because it is potentially null.
 - 'SchedulerBinding' is from 'package:flutter/src/scheduler/binding.dart' ('../../../development/flutter/packages/flutter/lib/src/scheduler/binding.dart').
Try calling using ?. instead.
      SchedulerBinding.instance.addPostFrameCallback((_) {
                                ^^^^^^^^^^^^^^^^^^^^
../../../development/flutter/.pub-cache/hosted/pub.dartlang.org/get-4.6.3/lib/get_navigation/src/snackbar/snackbar.dart:452:31: Error: Method 'addPostFrameCallback' cannot be called on 'SchedulerBinding?' because it is potentially null.
 - 'SchedulerBinding' is from 'package:flutter/src/scheduler/binding.dart' ('../../../development/flutter/packages/flutter/lib/src/scheduler/binding.dart').
Try calling using ?. instead.
    SchedulerBinding.instance.addPostFrameCallback(
                              ^^^^^^^^^^^^^^^^^^^^
../../../development/flutter/.pub-cache/hosted/pub.dartlang.org/get-4.6.3/lib/get_state_manager/src/rx_flutter/rx_notifier.dart:130:31: Error: Method 'addPostFrameCallback' cannot be called on 'SchedulerBinding?' because it is potentially null.
 - 'SchedulerBinding' is from 'package:flutter/src/scheduler/binding.dart' ('../../../development/flutter/packages/flutter/lib/src/scheduler/binding.dart').
Try calling using ?. instead.
    SchedulerBinding.instance.addPostFrameCallback((_) => onReady());
                              ^^^^^^^^^^^^^^^^^^^^
../../../development/flutter/.pub-cache/hosted/pub.dartlang.org/get-4.6.3/lib/get_state_manager/src/simple/get_controllers.dart:90:29: Error: Method 'addObserver' cannot be called on 'WidgetsBinding?' because it is potentially null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../development/flutter/packages/flutter/lib/src/widgets/binding.dart').
Try calling using ?. instead.
    WidgetsBinding.instance.addObserver(this);
                            ^^^^^^^^^^^
../../../development/flutter/.pub-cache/hosted/pub.dartlang.org/get-4.6.3/lib/get_state_manager/src/simple/get_controllers.dart:96:29: Error: Method 'removeObserver' cannot be called on 'WidgetsBinding?' because it is potentially null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../development/flutter/packages/flutter/lib/src/widgets/binding.dart').
Try calling using ?. instead.
    WidgetsBinding.instance.removeObserver(this);
                            ^^^^^^^^^^^^^^
../../../development/flutter/.pub-cache/hosted/pub.dartlang.org/get-4.6.3/lib/get_navigation/src/router_report.dart:53:31: Error: Method 'addPostFrameCallback' cannot be called on 'WidgetsBinding?' because it is potentially null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../development/flutter/packages/flutter/lib/src/widgets/binding.dart').
Try calling using ?. instead.
      WidgetsBinding.instance.addPostFrameCallback((_) {
                              ^^^^^^^^^^^^^^^^^^^^

FAILURE: Build failed with an exception.

* Where:
Script '/home/ibrahimdev/development/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 1102

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command '/home/ibrahimdev/development/flutter/bin/flutter'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 18s

7 Answers

Some changes happened in null-safety properties at the last upgrade of Flutter 3.0.0. There are a few approaches to a solution.

1-) If you upgraded the latest Flutter version and if your error is in the Flutter Core codes you must try;

- dart fix --apply

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

If this solution doesn't fix any error you can downgrade the Flutter version;

- flutter downgrade v2.10.5

2-) If you didn't upgrade or the error is in the third-party package;

  1. The developer of this package may have upgraded this package to the recent version of Flutter. You can upgrade packages with;

    flutter pub upgrade --major-versions
    
  2. If the developer hasn't upgraded yet, you must go to this package's Github repo and look at the issue tab. Maybe someone opened an issue and the dev answered the correct running version. For example, GetX's latest compatible version with Flutter 2.10.5 is 4.6.1. So for applying this version you can just use the version constraint (remove ˆ).

Update

get: ˆ4.6.1

to

get: 4.6.1

in your pubspec.

(Ref: https://github.com/jonataslaw/getx/issues/2356)

ran into the same problem and solved it by updating my flutter version to version 3.0. Run flutter upgrade in your console and reopen vscode(or your editor). check the bottom right (on vscode) to ensure it has been updated to version 3.0.0

Nullability for SchedulerBinding, WidgetsBinding and other clases changed in Flutter 3.0.0. So several packages update their code to remove warning.

If during your development you did not update any package you may wonder why you are getting the error.

Make sure that you are running flutter pub get and not flutter upgrade.

To fix the problem:

  • Run flutter clean
  • Delete pubspeck.lock if exists
  • Run flutter get

If you still see the error. Maybe the package still updating when you run flutter get.

So you can remove the ^ for example: flutterfire_ui: ^0.4.0+5 will become flutterfire_ui: 0.4.0+5

i get the same problem.And you need to upgrade flutter 3.0.0 version.

in my case i got problem with widgets binding from auto_route_navigator from my debug console vscode. thats mean its reference into auto_route: ^4.0.0

: Error: Method 'addPostFrameCallback' cannot be called on 'WidgetsBinding?' because it is potentially null.
../…/widgets/auto_route_navigator.dart:47
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../../../dev/flutter/packages/flutter/lib/src/widgets/binding.dart').
package:flutter/…/widgets/binding.dart:1
    Try calling using ?. instead.
          WidgetsBinding.instance.addPostFrameCallback((_) {
                                  ^^^^^^^^^^^^^^^^^^^^

so search issue on package github and i found these https://github.com/Milad-Akarie/auto_route_library/issues/1070#issuecomment-1126674802

before

auto_route: ^4.0.0

after [solved]

auto_route: "4.0.0"

other solution just upgrade your flutter into latest stable version ( flutter 3 )

Upgraded flutter to 3.0.0 from 2.10.3 help me fix this error.

Upgrade flutter to 3.0.0 it will resolve this issue

Related