Home > OS >  Flutter: Error thrown in some case using ScrollablePositionedList
Flutter: Error thrown in some case using ScrollablePositionedList

Time:08-30

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?

CodePudding user response:

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.

  • Related