I am trying to turn the sound on and off by clicking the same button, but I get a "The method 'loop' was called on null." error. I could not understand its logic because I am an amateur. Off topic If you have any suggestions for me, I am ready to listen for learning flutter.

class _MyHomePageState extends State<MyHomePage> {
  AudioPlayer player;
  AudioCache cache;
  bool playing = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: IconButton(
          iconSize: 50,
          icon: Icon(
            Icons.play_arrow,
            color: Colors.pink,
          ),
          onPressed: () {
            if (playing = true) {
              cache.loop("drum13.wav", volume: 3);
            } else {
              player.pause();
            }
          },
        ),
      ),
    );
  }
}


Solution 1: blokberg

You should call the constructors.

AudioCache cache = AudioCache();
AudioPlayer player = AudioPlayer();