google / flutter.widgets

https://pub.dev/packages/flutter_widgets
BSD 3-Clause "New" or "Revised" License
1.37k stars 477 forks source link

Getting Null check operator used on a null value when trying to run from initState() ? #291

Open ElixirMike opened 3 years ago

ElixirMike commented 3 years ago

In my scenario, When the widget loads, I want it to automatically jump/scroll to a specific selected index in the list. If I do this with a button trigger, it correctly scrolls to the spot in the list.

However, when I try to to do this from initState, It throws the error "Null check operator used on a null value".

final ItemScrollController itemScrollController = ItemScrollController();
final ItemPositionsListener itemPositionsListener =  ItemPositionsListener.create();

initState

    @override
    initState(){
    itemScrollController.jumpTo(index: 5);
    super.initState();
    }

In my widget tree?

ScrollablePositionedList.builder(
                itemCount: widget.tables.length,
                itemBuilder: (context, index) => ListTile(
                    leading: GBIcon(
                        hexColor: widget.tables[index].tableId == widget.currentTable.tableId
                            ? '#FFFFFF'
                            : widget.block.color,
                        icon: widget.tables[index].icon,
                        size: 24),
                    title: Text(widget.tables[index].name),
                    selected: widget.tables[index].tableId == widget.currentTable.tableId,
                    selectedTileColor: hexStringToColor(widget.block.color),
                    onTap: () async {
                      widget.loadTable(widget.tables[index].tableId.toString());
                    },
                  ),
                itemScrollController: itemScrollController,
                itemPositionsListener: itemPositionsListener,
              ) 
jamesderlin commented 3 years ago

jumpTo must not be called unless isAttached is true. (This is mentioned by the isAttached documentation, but it probably should be mentioned by the jumpTo/scrollTo documentation too.)

If you want to set an initial scroll index, you should be supplying an argument for the initialScrollIndex parameter in ScrollablePositionedList's constructor.