dynamic initialPage not working in PageController

This is my page view code :

late PageController pageController;
int pageInd = 0;

class DetailedWorkoutScreenStatePageView extends StatefulWidget {
  int initialPage = 0;
  DetailedWorkoutScreenStatePageView({super.key, required this.initialPage});

  State<DetailedWorkoutScreenStatePageView> createState() =>

class _DetailedWorkoutScreenStatePageViewState
    extends State<DetailedWorkoutScreenStatePageView> {
  void initState() {
    pageController = PageController(initialPage: widget.initialPage); // adding the index to PageController

  Widget build(BuildContext context) {
    return Scaffold(
        body: PageView.builder(
      onPageChanged: (ind) {
        setState(() {
          pageInd = ind;
      itemCount: allBodyPartWorkouts.length,
      itemBuilder: (context, ind) {
        WorkoutModel data = allBodyPartWorkouts[ind];
        return DetailedWorkoutScreen(
          name: data.name,
          bodyPart: data.bodyPart,
          gifUrl: data.gifUrl,
          target: data.target,
          instructions: data.instructions,

The initialPage variable is getting the right index still the page view is showing the first page.
where did i get it wrong? I really don’t understand.

>Solution :

You have not assigned a controller to your PageView. Add pageController to the controller field like this:

  controller: pageController,

Leave a Reply