Prudhvi
Prudhvi

Reputation: 116

Flutter Error "PageController.page cannot be accessed before a PageView is built with it."

In PageViewBuilder, instead of swipe gesture I want to move to the next page using onTap on an ArgonTimerButton which I found on pub.dev. I created a controller and used controller.nextPage() but it gives me this error:

'positions.isNotEmpty': PageController.page cannot be accessed before a PageView is built with it.

My code:

 final controller = PageController();
   @override
   Widget build(BuildContext context) {
   MediaQueryData size = MediaQuery.of(context).size;
   return  PageView.builder(
      physics: NeverScrollableScrollPhysics(),
      controller: PageController(),
      itemCount: widget.workoutExcercises.length,
      itemBuilder: (context, index) {
        return 

              //other widgets

              ArgonTimerButton(
                initialTimer: 3, // Optional
                height: 50,
                width: size.width * 0.55,
                minWidth: width * 0.40,
                color: Colors.white,
                borderRadius: 5.0,
                curve: Curves.easeInToLinear,
                child: Text(
                  "Done?",
                  style: TextStyle(
                      color: Colors.black,
                      fontSize: 24,
                      fontWeight: FontWeight.w700),
                ),
                loader: (timeLeft) {
                  return Text(
                    "Rest Time | $timeLeft",
                    style: TextStyle(
                        color: Colors.black,
                        fontSize: 18,
                        fontWeight: FontWeight.w700),
                  );
                },
                onTap: (startTimer, btnState) {
                  if (btnState == ButtonState.Idle) {
                    controller.nextPage(duration: Duration(milliseconds:1000), curve: Curves.easeInToLinear);
                  }
                },
              ),
            ],
          ),
        );
      },
    );

How do I solve this?

Upvotes: 0

Views: 3717

Answers (1)

Raouf Rahiche
Raouf Rahiche

Reputation: 31386

You are creating the controller but you are not using it.

Instead of :

controller: PageController(),

Do:

controller: controller,

Upvotes: 3

Related Questions