Iliass20
Iliass20

Reputation: 87

PageController.page cannot be accessed before a PageView is built with it . Failed assertion: line 172 pos 7: 'positions.isNotEmpty'

I'm trying to use Page View builder, and I want to scroll horizontally between items which contain an image and title and a body for description. but when I use a FloatingActionButton to control the page view By using PageController it shows me the error in the title
enter image description here

this is my code

    class On_Boarding extends StatelessWidget {

   List <Boarding_model> boarding = [
     Boarding_model(
         image: 'assets/images/onbording1.png',
         title: 'Screen title 1',
         body: 'Screen body 1',
     ),
     Boarding_model(
         image: 'assets/images/onbording1.png',
         title: 'Screen title 2',
         body: 'Screen body 2',
     ),
     Boarding_model(
         image: 'assets/images/onbording1.png',
         title: 'Screen title 3',
         body: 'Screen body 3',
     ),
   ];

   var boardController = PageController();
   
   @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(),
      body:Padding(
        padding: const EdgeInsets.all(23.0),
        child: Column(
          children: [
            Expanded(
              child: PageView.builder(
                physics: BouncingScrollPhysics(),
                itemBuilder:(context ,index)=> buildBoardingItem(boarding[index]),
                itemCount:boarding.length ,
              ),
            ),
            SizedBox(
              height: 40.0,
            ),
            Row(
              children: [
                Text(
                  'indicator'
                ),
                Spacer(),
                FloatingActionButton(
                  onPressed: (){
                    boardController.nextPage(
                        duration: Duration(
                          milliseconds: 750,
                        ),
                        curve: Curves.fastLinearToSlowEaseIn,
                    );
                  },
                  child: Icon(
                    Icons.arrow_forward_ios,
                  ),
                ),
              ],
            ),
          ],
        ),
      ) ,
    );
  }

Upvotes: 0

Views: 327

Answers (1)

Tushar Patel
Tushar Patel

Reputation: 686

you are creating a controller but not using it in pageView.builder

Do Changes:

PageView.builder(
                controller: boardController,
                physics: BouncingScrollPhysics(),
                itemBuilder:(context ,index)=> buildBoardingItem(boarding[index]),
                itemCount:boarding.length ,
              );

Upvotes: 2

Related Questions