title: Flutter: Listview get full size of scrollcontroller after adding item to list & scroll to end
body: <p><a href="https://stackoverflow.com/a/44142234/3249027">This</a> post describes a very similar problem, but the answer there doesn't solve all problems:</p>
  
  <p>I have a potentially long List, where the user can add new items (on at a time). After/On add, the list should scroll to its end.</p>
  
  <p>(Btw no, <code>reverse: true</code> is not an option)</p>
  
  <p>After reading the other post, I understood using <code>SchedulerBinding.instance.addPostFrameCallback((_) =&gt; scrollToEnd());</code> should to the trick b/c the new lists <code>maxScrollExtent</code> will be correct.</p>
  
  <p>But it doesn't work reliably:
  When already scrolled to the end of the list or near the end everything's ok.
  But when the list is scrolled to its start (or some way from the end) when adding a new item, the list gets scrolled, but the scrollposition is off by exactly one item - the newest one.</p>
  
  <p>I think it might have something to do with the <code>ListView.builder</code> not keeping all children alive - but how to solve it?</p>
  
  <p>Oh and bonus question: just discovered another very strange behaviour: after adding two items the last one is a little bit out of view but the list isn't scrollable - which is strange. But even stranger is that on the next add-item-click the list scrolls this tiny bit - but without ever creating the new item!?</p>
  
  <p>Here a complete example:</p>
  
  <pre><code>import 'package:flutter/foundation.dart';
  import 'package:flutter/material.dart';
  import 'package:flutter/scheduler.dart';
  
  void main() {
    runApp(MyList());
  }
  
  class MyList extends StatefulWidget {
    MyList({Key key}) : super(key: key);
  
    @override
    _MyListState createState() =&gt; _MyListState();
  }
  
  var items = List&lt;String&gt;.generate(8, (i) =&gt; "Item $i");
  
  class _MyListState extends State&lt;MyList&gt; {
    static ScrollController _scrollController = ScrollController();
  
    void add() {
      setState(() {
        items.add("new Item ${items.length}");
        print(items.length);
      });
      SchedulerBinding.instance.addPostFrameCallback((_) =&gt; scrollToEnd());
    }
  
    void scrollToEnd() {
      _scrollController.animateTo(_scrollController.position.maxScrollExtent,
          duration: const Duration(milliseconds: 350), curve: Curves.easeOut);
    }
  
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: "List",
        home: Scaffold(
            appBar: AppBar(
              title: Text("List"),
            ),
            body: ListView.builder(
              controller: _scrollController,
              itemCount: items.length,
              shrinkWrap: true,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text('${items[index]}'),
                );
              },
            ),
            bottomSheet: Container(
                decoration: BoxDecoration(
                    border:
                        Border(top: BorderSide(color: Colors.black, width: 1))),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    FloatingActionButton(
                      onPressed: () {
                        add();
                      },
                      child: Icon(Icons.add),
                    )
                  ],
                ))),
      );
    }
  }
  </code></pre>