Cannot Navigate To Next Page PageView

Issue

I am using a pageview builder but cannot iterate through the pages. I keep getting this error:

“The method ‘animateToPage’ was called on null.
Receiver: null
Tried calling: animateToPage(1, curve: Instance of ‘Cubic’, duration: Instance of ‘Duration’)”

Here is what I’m calling:

 PageView.builder(
                        itemBuilder: (context, i) {
                          return _prodctReview2(context, i);
                        },
                        itemCount: currentReview.order.orderDetails.length,
                        controller: _pageController,
                      )

Here is the function I’m calling:

 Future nextPage(i) async {
    var next = currentPageValue + 1;

    print(i);

    if (i == currentReview.order.orderDetails.length) {
      Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => DriverReviewPage(
                    currentReview: recommendedProducts[i],
                  )));
    } else {
      _pageController.jumpToPage(
        next,
      );
    }
  }

When I call the function I get this error

Unhandled Exception:
NoSuchMethodError: The method ‘jumpToPage’ was called on null.
Receiver: null Tried calling: jumpToPage(1)

I’m not sure why because the pages are attached.

Solution

According to the Error, it is possible that the _pageController is not properly initialized. For example, in a stateful widget, you have to initialize the _pageController variable in the initState method.

@override
void initState(){
    _pageController = PageController(initialPage: 0);
}

Answered By – Frenco

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *