mrcendre / motion

A Flutter plugin to apply a gyroscope-based motion effect to widgets.
GNU General Public License v3.0
39 stars 15 forks source link

Error: Method `addPostFrameCallback` cannot be called on `WidgetsBinding?` because it is potentially null. #2

Closed sebastianbuechler closed 2 years ago

sebastianbuechler commented 2 years ago

When running with Flutter 2.10.5 I get an error because of null safety. Is this package really compatible with Flutter version < 3.0.0?

Launching lib\main.dart on Emulator in debug mode...
lib\main.dart:1
/C:/src/flutter/.pub-cache/hosted/pub.dev/motion-1.1.1/lib/src/utils/measure.dart:20:29: Error: Method 'addPostFrameCallback' cannot be called on 'WidgetsBinding?' because it is potentially null.
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/binding.dart').
package:flutter/…/widgets/binding.dart:1
Try calling using ?. instead.
    WidgetsBinding.instance.addPostFrameCallback((_) {
                            ^^^^^^^^^^^^^^^^^^^^
3

FAILURE: Build failed with an exception.

* Where:
Script 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1102

* What went wrong:
Execution failed for task ':app:compileFlutterBuildProdDebug'.
> Process 'command 'C:\src\flutter\bin\flutter.bat'' 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 1m 1s

I saw for another package https://stackoverflow.com/questions/72256150/flutter-error-method-addpostframecallback-cannot-be-called-on-widgetsbinding that this issue was appearing as part of Flutter v2 vs v3.

mrcendre commented 2 years ago

Hi @sebastianbuechler, thanks for your feedback.

The plugin is indeed supposed to work with both Flutter 2 and 3. The latter introduced a null-safety change to the WidgetsBinding class, removing nullability for the instance field.

To avoid breaking support for Flutter 2, I put back the null-safety operator ?, as it will only trigger a warning on Flutter 3 (more informations here). Plus, this warning is now inhibited by ambiguating instance back and accessing it just like in Flutter 2.

Closing this issue as it is now fixed in version 1.1.3. Thanks again !

sebastianbuechler commented 2 years ago

Thank you!