Search code examples
flutterdartflutter-dependenciesflame

Flutter: Fatal Error: Callback lookup failed! (with audioplayers package)


The following snippet is the button by clicking which, a short beep plays:

FlatButton(
  onPressed: () => {
    final player = new AudioCache();
    player.play('note1.wav');
  }
  child: Text('Click to ding!'),
),

The problem I'm facing is whenever I click this button, although sound plays correctly, I get this error in terminal: flutter: Fatal Error: Callback lookup failed! I wanna know what it is and what I should do?

I checked the package's issue page but didn't find anything helpful.

Thanks


Solution

  • Looks like a bug where "Fatal Error: Callback lookup failed!" is printed in the logs if no callback method was defined via monitorNotificationStateChanges.

    By reading the source code it seems like this doesn't have any incidence.

    But a way to avoid it is indeed to set that callback, and it has to be defined outside your class like so:

    import 'package:audioplayers/audioplayers.dart';
    
    class YourStatefulWidget extends StatefulWidget {
      @override
      _YourStatefulWidgetState createState() => _YourStatefulWidgetState();
    }
    
    class _YourStatefulWidgetState extends State<YourStatefulWidget> {
      ...
    
      @override
      void initState() {
        super.initState();
    
        if (Platform.isIOS) {
          // to avoid getting "Fatal Error: Callback lookup failed!"
          audioPlayer.monitorNotificationStateChanges(audioPlayerHandler);    
        }
      }
    
      ...
    }
    
    // must be defined globally
    void audioPlayerHandler(AudioPlayerState value) => null;