Search code examples
flutterdartscrollviewscrollbarsinglechildscrollview

Flutter The Scrollbar's ScrollController has no ScrollPosition attached


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?


Solution

  • 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
        ....
      )
    )