Search code examples
flutterdartclone

The method 'animateToPage' was called on null


I'm trying to clone Instagram and the Navigation Bar dos not navigate but it through me an error The method 'animateToPage' was called on null.

this is the code for it

 Scaffold  buildHomeScreen(){
    return Scaffold(
      body: PageView(
        children: <Widget>[
          TimeLinePage(),
          SearchPage(),
          UploadPage(),
          NotificationsPage(),
          ProfilePage(),

        ],
        controller: pageController,
        onPageChanged: whenPageChanges,
        physics: NeverScrollableScrollPhysics(),
      ),
      bottomNavigationBar: CupertinoTabBar(
        currentIndex: getPageIndex,
        onTap: onTapChangePage,
        backgroundColor: Theme.of(context).accentColor,
        activeColor: Colors.white,
        inactiveColor: Colors.blueGrey,
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home)),
          BottomNavigationBarItem(icon: Icon(Icons.search)),
          BottomNavigationBarItem(icon: Icon(Icons.photo_camera, size: 37.0,)),
          BottomNavigationBarItem(icon: Icon(Icons.favorite)),
          BottomNavigationBarItem(icon: Icon(Icons.person)),


        ],
      ),

    );

and those are the methods i use

 whenPageChanges(int pageIndex){
    setState(() {
      this.getPageIndex = pageIndex;
    });

  }
  onTapChangePage(int pageIndex){
    pageController.animateToPage(pageIndex, duration:Duration(milliseconds: 300), curve: Curves.bounceInOut);
  }

Solution

  • Are you sure the pageController is initialized in the right place? You also need to remember to cancel it in dispose. Show me the part of the code where you create the controller.