'Flutter ListView resetting to top of list

I'm working on an app that gets the user to take a list of measurements. I use ListView to display a list of measurements. When the the user clicks on a list item it takes them to a new page, they enter the measurement, hit save and then in the save method I do Navigator.pop(context) back to the list. It all works but there is a usability problem.

If you tap a list tile and then use the app bar to go back it returns to the same scroll position in the ListView. If you enter some data then hit Save it returns to the top of the list. Even though I'm using Navigator.pop(context) in the save method. You can imagine returning to the top each time is pretty painful when the list of requirement measurements is quite long. I guess is maybe its something to do with the fact that in the Save method I also update the model with which the list is built on so its kind of no longer the same list??

EDIT

I'm still not getting there and now I have an issue where the itemScrollController is not attached when I want to call it. Some code will hopefully help:

class ListContents extends StatefulWidget {
  ListContents({
    Key key,
  }) : super(key: key);

  @override
  _ListContentsState createState() => _ListContentsState();
}

class _ListContentsState extends State<ListContents> {
  ItemScrollController itemScrollController;
  @override
  void initState() {
    super.initState();
    itemScrollController = ItemScrollController();
  }

I set up the scroll controller in init state. I set up a button to test this. I can run this when the page loads:

void jump(jumpIndex) {
    itemScrollController.jumpTo(index: jumpIndex);
}

When I click that button it will jump to what ever index is passed.

I need to do this jumpTo when popping back from the previous page. I have this code in the list tiles. This loads an input page. - I was hoping to run the jumpTo method after it's popped.

onTapped: () async {
   await Navigator.of(context).push(
      MaterialPageRoute(
         builder: (context) =>
            ChangeNotifierProvider<MeasureInstance>.value(   
                value: _instance,
                 child: InputOne(
                          index: index,
                 ),
             ),
           ),
        );

     await _database.setInstance(_instance);
     print(itemScrollController.isAttached);
     itemScrollController.jumpTo(index: index);
},

When I pop back to the list page I see 'false' printed in the console and then an error that _jumpTo was called on null



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source