Execute scrollTo function automatically – Flutter

Issue

I’m using flutter scrollable positioned list package. There is a method called scrollTo which let you scroll to specific index in the list.

What I’m trying to achieve is that scrollTo function should execute automatically as soon as user land up on the screen.

I tried to call it in the initState but it throws the following error

‘_scrollableListState != null’: is not true

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    startScroll();
  }

  void startScroll(){
    itemScrollController.scrollTo(index: 5, duration: const Duration(seconds: 2));
  }

it works fine if I trigger the function through button click but that’s not what I’m trying to achieve

Solution

Try the following

WidgetsBinding.instance.addPostFrameCallback((_) => startScroll());

this function performs startScroll after UI build is complete

Answered By – Abdallah A. Odeh

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published