skip to Main Content

Can’t undersand what i’m doing wrong. audio_service library.

void initState() {
    super.initState();
    AudioHandler.mediaItem.value((MediaItem? item) {
      setState(() {
        _currentMediaItem = item;
      });
    });
  }

issue: Instance member ‘mediaItem’ can’t be accessed using static access.

I can’t find anything about this in official documentation. In documentation of audio_service said what i need to identify variables as static. I was trying but can’t fix that.

2

Answers


  1. Because mediaItem is a getter, and it’s not a static getter. This is why you get the error.

    Login or Signup to reply.
  2. The error you’re seeing is because you’re trying to access an instance member mediaItem as if it were a static member of the AudioHandler class.

    Here’s how you can resolve the issue:

    1. Ensure you have an instance of AudioHandler: Instead of accessing mediaItem as a static member, you should first get an instance of AudioHandler and then access its mediaItem member.
    AudioHandler audioHandlerInstance = ...; // Get your AudioHandler instance here
    audioHandlerInstance.mediaItem.value((MediaItem? item) {
      setState(() {
        _currentMediaItem = item;
      });
    });
    
    1. Check the documentation: It’s possible that the documentation or the library might have been updated. Always ensure you’re looking at the latest version of the documentation and that your library is up-to-date.

    2. Static members: If the documentation mentions marking variables as static, it typically means that those variables belong to the class itself and not to any particular instance of the class. However, in this case, mediaItem seems to be an instance member, so you should not be accessing it statically.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search