Home > Net >  Execute scrollTo function automatically - Flutter
Execute scrollTo function automatically - Flutter

Time:05-27

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

CodePudding user response:

Try the following

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

this function performs startScroll after UI build is complete

  • Related