28

I get this error when using the AppBar:

The Scrollbar's ScrollController has no ScrollPosition attached.

This is my CustomScrollBar:

class CustomScrollBar extends StatelessWidget {
  final Widget child;
  final ScrollController scrollController;

  const CustomScrollBar({
    required this.scrollController,
    required this.child,
  });

  @override
  Widget build(BuildContext context) {
    return RawScrollbar(
      thumbColor: AppColors.gray,
      radius: Radius.circular(8),
      thickness: 4,
      isAlwaysShown: true,
      controller: scrollController,
      child: child,
    );
  }
}

I should be always visible. And this is how I use it:

child: CustomScrollBar(
              scrollController: _scrollControllerForScrollBar,
              child: SingleChildScrollView(
                controller: _scrollControllerForScrollBar,
                child: Padding(
                  padding: EdgeInsets.all(7.0.scaled),
                  child: Container(
                    width: double.infinity,
                    child: Text(
                      'any text bla bla bla \n\n\n this is a lot of \n text \n .'
                    ),
                  ),
                ),
              ),
            ),

As you can see both the ScrollBar and the SingleChildScrollView use the same ScrollController. I have no idea why this error occurs. Any idea what I am missing here?

Chris
  • 1,828
  • 6
  • 40
  • 108

6 Answers6

61

I was using a ScrollBar with a ListView widget and was getting this error in the debug console. To get rid of it, I had to set the ScrollController on both widgets.

final yourScrollController = ScrollController();

Scrollbar(
  isAlwaysShown: true,
  thickness: 10,
  controller: yourScrollController, // Here 
  child: ListView.builder(
    padding: EdgeInsets.zero,
    scrollDirection: Axis.vertical,
    controller: yourScrollController, // AND Here
    itemCount: yourRecordList?.length
    ....
  )
)
jaredbaszler
  • 3,941
  • 2
  • 32
  • 40
6

Let's assume you're using both Scrollbar widget and ListView widget as a child of the scrollbar - Both of them should have the same controller as :

ScrollController _controller = ScrollController();

     Scrollbar(
              controller: _controller,
              child: ListView(
                controller: _controller,
           ....), // ListView
    ) // Scrollbar
2

Remove the scrollController entirely and that should fix the problem. that is how I fixed my code so your code should look something like this

class CustomScrollBar extends StatelessWidget {


 final Widget child;

  const CustomScrollBar({
    required this.child,
  });

  @override
  Widget build(BuildContext context) {
    return RawScrollbar(
      thumbColor: AppColors.gray,
      radius: Radius.circular(8),
      thickness: 4,
      isAlwaysShown: true,
      child: child,
    );
  }
}

and the second part should be like this

child: CustomScrollBar(
              child: SingleChildScrollView(
                child: Padding(
                  padding: EdgeInsets.all(7.0.scaled),
                  child: Container(
                    width: double.infinity,
                    child: Text(
                      'any text bla bla bla \n\n\n this is a lot of \n text \n .'
                    ),
                  ),
                ),
              ),
            ),
wilhit
  • 36
  • 5
1

If you use nested scroller like this:

 Expanded(
                          flex: 2,
                          child: Center(
                            child: Scrollbar(
                              child: SingleChildScrollView(
                                primary: true,
                                child: Scrollbar(
                                  child: SingleChildScrollView(
                                    scrollDirection: Axis.horizontal,
                                   .....)

You can fix the issue adding this line to first Scrollable view:

 primary: true

The UI plotter wants know start position for the scroller both on mobile and low resolutions on desktop with size:

MediaQuery.of(context).size.shortestSide < 600
berkb
  • 542
  • 6
  • 21
0

The following assertion was thrown during a scheduler callback: The Scrollbar's ScrollController has no ScrollPosition attached

A Scrollbar cannot be painted without a ScrollPosition.

The Scrollbar attempted to use the provided ScrollController. This ScrollController should be associated with the ScrollView that the Scrollbar is being applied to. When providing your own ScrollController, ensure both the Scrollbar and the Scrollable widget use the same one.

This is flutter's bug, https://github.com/flutter/flutter/issues/82573.

It is fixed on the flutter master branch:

flutter channel master
flutter upgrade
ouflak
  • 2,458
  • 10
  • 44
  • 49
0

For this error, you have to define same 1 scrollController for both ScrollBar and SingleSchildScrollView/ListView:

For example from my prj:

  final ScrollController _scrollController = ScrollController(); //define just 1 controller
  final _currencyFormatter = NumberFormat('#,##0.00', 'vi_VN');
  final double _safeSpaceToShowToolTip = 1.4;
  final double _fixedChartHeight = 350.0;
  int _touchedIndex = -1;

  @override
  Widget build(BuildContext context) {
    return Scrollbar(
      controller: _scrollController, // and use it both here
      isAlwaysShown: true,
      trackVisibility: true,
      radius: Radius.circular(6.0),
      child: SingleChildScrollView(
        controller: _scrollController, // AND HERE
        scrollDirection: Axis.horizontal,
        physics: BouncingScrollPhysics(),
        child: Container(
          margin: EdgeInsets.only(bottom: R.dimens.mediumSpacing),
          height:
              R.dimens.getSpecificSize(_fixedChartHeight, isForHeight: true),
          width: R.dimens
              .getSpecificSize(_fixedChartHeight * 1.9, isForHeight: false),
          child: BarChart(
            _barChartData(),
          ),
        ),
      ),
    );
  }
Nghien Nghien
  • 189
  • 2
  • 6