SimformSolutionsPvtLtd / audio_waveforms

Use this plugin to generate waveforms while recording audio in any file formats supported by given encoders or from audio files. We can use gestures to scroll through the waveforms or seek to any position while playing audio and also style waveforms
https://pub.dev/packages/audio_waveforms
MIT License
283 stars 150 forks source link

WidgetsBindingObserver build failed #79

Closed Technorocker closed 2 years ago

Technorocker commented 2 years ago

I was looking for a solution for this plugin as I was getting fails while trying to build out an apk. I jsut updated to newest version from 0.1.1 to 0.1.4 the verifyresource issue is indeed gone now but now I have a new issue with widgetbindings. This is the error I'm getting when build fails...

../flutter/.pub-cache/hosted/pub.dartlang.org/audio_waveforms-0.1.4/lib/src/audio_waveforms.dart:176:29: Error: Method 'addPostFrameCallback' cannot be called on 'WidgetsBinding?' because it is potentially null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../flutter/packages/flutter/lib/src/widgets/binding.dart').  
Try calling using ?. instead.
    WidgetsBinding.instance.addPostFrameCallback((_) {
                            ^^^^^^^^^^^^^^^^^^^^
../flutter/.pub-cache/hosted/pub.dartlang.org/audio_waveforms-0.1.4/lib/src/controllers/recorder_controller.dart:203:29: Error: Method 'addPostFrameCallback' cannot be called on 'WidgetsBinding?' because it is potentially null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../flutter/packages/flutter/lib/src/widgets/binding.dart').  
Try calling using ?. instead.
    WidgetsBinding.instance.addPostFrameCallback((_) {

The only 2 places I use the audiowaveform I also use a timer in the widget so I use the WidgetsBindingObserver on the class.

So after checking out the two errors a bit more I went to the 2 locations that you use addPostFrameCallback and added an ! at the end of instance like so for the first file reference...

    WidgetsBinding.instance!.addPostFrameCallback((_) {
      setState(() {});
    });

and again for the second file reference

    WidgetsBinding.instance!.addPostFrameCallback((_) {
      shouldClearLabels = false;
      notifyListeners();
    });

After I added those 2 exclamation marks I am able to build the apk now

Ujas-Majithiya commented 2 years ago

@Technorocker I think you are using lower version of flutter. With flutter 3.0, there was change in nullability of WidgetsBinding.instance. So, we no longer require adding ? or ! after WidgetsBinding.instance. I recommend updating your flutter sdk.

Technorocker commented 2 years ago

Ah ok, Ya im not upgrading past the version im using for my current app. Last time i tried that I spent an entire day trying to figure out how to downgrade my flutter/gradle/android/blah after most the plugins I'm using werent compatible unfortunately.

But good to know, thank you for response.

Ujas-Majithiya commented 2 years ago

@Technorocker #103 will fix the errors you were getting for postFrameCallback.