androidflutterandroid-studiodartandroid-music-player

Method not found: 'miniPlayer'


I have a page with name playerPage which contains the bottomNavigationBar which has got mini player and navigation items respectively. I also have a MyHomePage which contains the songs to be played. I created a constructor with Function name miniPlayer in MyHomePage to call _miniPlayer method in the playerPage but I am getting an error that says Method not found: 'miniPlayer'.

MyHomePage
class MyHomePage extends StatefulWidget {
   final Function miniPlayer;
  const MyHomePage(this.miniPlayer);

 // const MyHomePage({Key? key}) : super(key: key);
  @override
  State<MyHomePage> createState() => _MyHomePageState(miniPlayer);
}

class _MyHomePageState extends State<MyHomePage> {
  final  Function miniPlayer;
  _MyHomePageState(this.miniPlayer);

Widget _recommended(context) {
  return Column(
    children: [

      StreamBuilder<QuerySnapshot>(
        stream: FirebaseFirestore.instance.collection("Worship").snapshots(),
        builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if(snapshot.hasData) {
            final snap = snapshot.data!.docs;
            return ListView.builder(
              shrinkWrap: true,
              primary: false,
              itemCount: snap.length,
              itemBuilder: (context, index) {
                         return Stack(
                    children: [
                      GestureDetector(
                        child:Container(

                          height:50,
                      width: MediaQuery.of(context).size.width,


                      child: Card(
                        child: Center(
                          child: Row (
                            children : [
                                                 Text(

                          snap[index]['name'],
                          textAlign: TextAlign.start,
                          style: const TextStyle(
                            color: Colors.black54,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                            SizedBox(width:50),
                            Text(

                              snap[index]['title'],
                              textAlign: TextAlign.start,
                              style: const TextStyle(
                                color: Colors.black54,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          ]
                       )),
                      )),
                        onTap: () {

                          var ur= snap[index]['url'];
                          var ti =snap[index]['title'];
                          var music =snap[index]['music'];

                         **miniPlayer(url:ur,le:ti,music:music);** Getting error here

                        },

                      )
                      ],
                //   ),
                 );
              },
            );
          } else {
            return const SizedBox();
          }
        },
      )
    ],

  );
}

PlayerPage
this is the miniPlayer method in the playerPage

 Widget miniPlayer(le,url,music) {
   this.le =le;
   this.url =url;
   this.music =music;
   setState(() {

   });
   if(widget.le.isEmpty) {
     return SizedBox();
   }
    return AnimatedContainer(
      duration: const Duration(milliseconds: 500),
      color: Colors.indigo,
      height: MediaQuery.of(context).size.height * 0.07,
      width: MediaQuery.of(context).size.width,
      child: Row(
        children: [
          Container(
            padding: EdgeInsets.only(left: 6),
            child: CircleAvatar(
              backgroundColor: Colors.indigo,
              radius: 20,
              child: IconButton(
                icon: Icon(
                  isPlaying ? Icons.pause : Icons.play_arrow_rounded,
                  color: Colors.white,
                ),
                onPressed: () async {
                  if (isPlaying) {
                    await audioPlayer.pause();
                  } else {
                    await audioPlayer.resume();
                  }
                },
              ),
            ),
          ),
          const SizedBox(
            width: 10,
          ),
          Container(
              padding: EdgeInsets.only(left: 5),
              width: MediaQuery.of(context).size.width * 0.85,
              child: Column(
                children: [
                  Expanded(
                      child: Marquee(
                    textDirection: TextDirection.ltr,
                    velocity: 30,
                    blankSpace: 90,
                    //pauseAfterRound: const Duration(seconds: 2),
                    text: widget.le,
                    style: const TextStyle(
                        color: Colors.white,
                        fontSize: 15,
                        fontWeight: FontWeight.bold),
                  ))
                ],
              ))
        ],
      ),
    );
  }

Solution

  • First of all, here it's not properly understood. It's a little bit messy. But one thing is make sure you pass it down the widget tree. And another thing is try not to pass properties through private constructors. You can easily access that property (aka. miniPlayer function) just by typing widget.miniPlayer. In that way you can be more sure that this function is available in the main widget class.

    Also you're returning an widget inside a function. Make sure you use it. Other wise that widget getting returned won't do anything inside a function.