US Office

1176 Shadeville Rd, Crawfordville Florida 32327, USA

 +1 (850) 780-1313

India Office

Office No. 501, Shree Ugati Corporate Park, Gandhinagar - 382421, Gujarat, India

[email protected]

How to Solve Scroll Controller Could Not Attached to Any Scroll Views In Flutter ?

How to Solve Scroll Controller Could Not Attached to Any Scroll Views In Flutter

How to Solve Scroll Controller Could Not Attached to Any Scroll Views In Flutter ?

ScrollBar Widget that can be dragged for quickly navigation through a vertical list. A ScrollBar Widget indicates which portion of a Scrollable Widget is actually visible. In this article, we will go through How to solve Scroll Controller could not attach to any Scroll Views In Flutter?

How to Solve Scroll Controller Could Not be Attached to Any Scroll Views In Flutter?

Users need to check if the ScrollController is attached to a scroll view by using its client’s property first.

if (_scrollController.hasClients) 
    _scrollController.jumpTo(50.0);

To set the initial position of a ScrollController, use the initialScrollOffset property:

_scrollController = ScrollController(initialScrollOffset: 50.0);

Delaying it is not the right solution. Better to wait till the tree is done building by using the below code snippet.

WidgetsBinding.instance
        .addPostFrameCallback((_){});

Users can also try giving below code snippets

@override
void initState(){
  super.initState();
  WidgetsBinding.instance.addPostFrameCallback((_){
    //write or call your logic
    //code will run when widget rendering complete
  });
}

This sometimes happens when you are attempting to bind a ScrollController to a widget that doesn’t actually exist (yet). So it attempts to bind, but there is no ListView/ScrollView to bind to.

Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: <Widget>[
    Expanded(
      flex: 8,
      child: Container(
        child: ListView.builder(
          controller: scrollController,
          itemCount: messages.length,
          itemBuilder: (context, index) {
            return MessageTile(message: messages[index]);
          }),
        ),
     ),
 ]),

first, declare the ListView and ScrollController

final scrollController = ScrollController(initialScrollOffset: 0);
ListView list = ListView.builder(
  controller: scrollController,
  itemCount: messages.length,
  itemBuilder: (context, index) {
    return MessageTile(message: messages[index]);
  }
);

then inside your build function just reference the already built list

Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: <Widget>[
    Expanded(
      flex: 8,
      child: Container(
         child: list,
      ),
    ),
  ]),
Future.delayed(Duration(milliseconds: <some time, ex: 100>), () {
         _scrollController.jumpTo(50.0);
        });

Initialize the scrollController:

ScrollController _scrollController = ScrollController();

Use the code below where you want to scroll:

SchedulerBinding.instance.addPostFrameCallback((_) {
  _scrollController.jumpTo(_scrollController.position.maxScrollExtent);
});

Conclusion:

In this article, we have been through How to solve the scroll controller could not attach to any scroll views in Flutter?

Thanks for Reading !!!

Keep Learning !!! Keep Fluttering !!!
Do share your valuable suggestion feedback for the same.

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

Post a Comment