Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter: Error thrown in some case using ScrollablePositionedList

I’m working on an indexed list, I found this lib: ScrollablePositionedList that does the job, when I call the function jumpTo/scrollTo within a button tap, it works but when I call it before the return in build, it throws error:

E/flutter ( 3018): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: 'package:scrollable_positioned_list/src/scrollable_positioned_list.dart': Failed assertion: line 236 pos 12: '_scrollableListState != null': is not true.

Here the sample:

@override
  Widget build(BuildContext context) {
    // ...
    ItemScrollController idxCtrl = ItemScrollController();
    listenable.listen(
      (p0) {
        idxCtrl.scrollTo(index: p0, duration: Duration(seconds: 1));//--> Throw error
    });
    // ...
    return Container(
    // ...
      IconButton(
        onPressed: () {
          idxCtrl.scrollTo(index: 0, duration: Duration(seconds: 1));//--> Working
        },
        icon: Icon(Icons.tab)),
    // ...
      ScrollablePositionedList.builder(
        itemScrollController: idxCtrl,
    // ...
    // ...
    )
    // ...

Does anyone know how to solve it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Define variable out of build method, so move idxCtrl to above of build

ItemScrollController idxCtrl = ItemScrollController();
@override
  Widget build(BuildContext context) {
...
}

what you did is try to define idxCtrl every time build method call so it can’t recognize it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading