shakky
shakky

Reputation: 474

Execute scrollTo function automatically - Flutter

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

Upvotes: 1

Views: 323

Answers (1)

Abdallah A. Odeh
Abdallah A. Odeh

Reputation: 1792

Try the following

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

this function performs startScroll after UI build is complete

Upvotes: 1

Related Questions