How to solve Failed assertion: line 110 pos 12: '_positions.isNotEmpty': ScrollController not attached to any scroll views

Viewed 4083

Also get the solution on StackOverflow but it's not working in my case

'_positions.isNotEmpty': ScrollController not attached to any scroll views

PageView throws '_positions.isNotEmpty': ScrollController not attached to any scroll views

Widget _buildIntroSlider() {
            return Container(
              padding: EdgeInsets.symmetric(horizontal: 46.0),
              child: Stack(
                alignment: AlignmentDirectional.bottomCenter,
                children: <Widget>[
                  PageView.builder(
                    scrollDirection: Axis.horizontal,
                    controller: _pageController,
                    onPageChanged: _onPageChanged,
                    itemCount: slideList.length,
                    itemBuilder: (ctx, i) =>
                        SlideItem(slide: slideList[i], bold: false),
                  ),
                 
                ],
              ),
            );
          }
1 Answers

Whenever you use the PageController, you need to keep in mind that if you try to access the PageController data, like the current index before the PageView with that specific controller is rendered, you will get this error. You can check if it is aleady built with

_pageController.hasClients

and if not, you cannot call any other methods of the controller otherwise it will throw an error.

Related