Repository: flutter
Issue: [Docs] Adding children outside of the cache extent to ListView does not update maxScrollExtent
Labels: framework,f: scrolling,d: api docs,d: stackoverflow,P2,team-framework,triaged-framework

<!-- Thank you for using Flutter!

     If you are looking for support, please check out our documentation
     or consider asking a question on Stack Overflow:
      * https://flutter.dev/
      * https://api.flutter.dev/
      * https://stackoverflow.com/questions/tagged/flutter?sort=frequent

     If you have found a bug or if our documentation doesn't have an answer
     to what you're looking for, then fill out the template below. Please read
     our guide to filing a bug first: https://flutter.dev/docs/resources/bug-reports
-->

## Steps to Reproduce

<!-- Please include full steps to reproduce so that we can reproduce the problem. -->

1. Execute `flutter run` on the code sample <!-- (see "Code sample" section below) -->
2. Tap the FAB.
3. You can see the viewport scrolls to right before the last item.

**Expected results:** <!-- what did you want to see? -->
- scrolls to the last item when the FAB is tapped.

**Actual results:** <!-- what did you see? -->
- scrolls to right before the last item when the FAB is tapped.

https://user-images.githubusercontent.com/52235899/145372818-5e2a9be9-3b2d-4eaa-8aef-1c4368aa1947.mov

<details>
<summary>Code sample</summary>

<!--
      Please create a minimal reproducible sample that shows the problem
      and attach it below between the lines with the backticks.

      To create it you can use `flutter create bug` command and update the `main.dart` file.

      Alternatively, you can use https://dartpad.dev/
      which is capable of creating and running small Flutter apps.

      Without this we will unlikely be able to progress on the issue, and because of that
      we regretfully will have to close it.
-->

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);
  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final _scrollController = ScrollController();
  int _childCount = 50;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('app bar'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            _childCount++;
          });

          WidgetsBinding.instance?.scheduleFrameCallback(
            (_) => WidgetsBinding.instance?.addPostFrameCallback(
              (_) => _scrollController.animateTo(
                _scrollController.position.maxScrollExtent,
                duration: const Duration(milliseconds: 200),
                curve: Curves.easeIn,
              ),
            ),
          );
        },
        child: const Icon(Icons.arrow_downward),
      ),
      body: SafeArea(
        child: RefreshIndicator(
          onRefresh: () {
            return Future.delayed(const Duration(seconds: 1));
          },
          child: ListView.builder(
            controller: _scrollController,
            padding: EdgeInsets.zero,
            itemCount: _childCount,
            itemBuilder: (context, index) {
              return ListTile(
                title: Center(
                  child: Text('ListTile $index'),
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}

```

</details>

<details>
  <summary>Logs</summary>

<!--
      Run your application with `flutter run --verbose` and attach all the
      log output below between the lines with the backticks. If there is an
      exception, please see if the error message includes enough information
      to explain how to solve the issue.
-->

```

```

<!--
     Run `flutter analyze` and attach any output of that command below.
     If there are any analysis errors, try resolving them before filing this issue.
-->

```
Analyzing flutter_pr...                                         
No issues found! (ran in 2.4s)
```

<!-- Finally, paste the output of running `flutter doctor -v` here. -->

```
[✓] Flutter (Channel stable, 2.8.0, on macOS 12.0.1 21A559 darwin-x64, locale ja-JP)
    • Flutter version 2.8.0 at /Users/takashi/fvm/versions/2.8.0
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision cf44000065 (12 hours ago), 2021-12-08 14:06:50 -0800
    • Engine revision 40a99c5951
    • Dart version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at /Users/takashi/Library/Android/sdk
    • Platform android-30, build-tools 30.0.2
    • ANDROID_HOME = /Users/takashi/Library/Android
    • ANDROID_SDK_ROOT = /Users/takashi/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.10.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)

[✓] VS Code (version 1.62.3)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.29.0

[✓] Connected device (2 available)
    • iPhone 11 Pro (mobile) • 1D9077E4-D995-48E1-B080-F02C395F94EC • ios            • com.apple.CoreSimulator.SimRuntime.iOS-15-0 (simulator)
    • Chrome (web)           • chrome                               • web-javascript • Google Chrome 96.0.4664.93
```

</details>


<!--
      Consider also attaching screenshots and/or videos to better
      illustrate the issue.

      You can upload them directly on GitHub.
      Beware that video file size is limited to 10MB.
-->

## notice
- **if I tap the FAB during scrolling, it works.**

https://user-images.githubusercontent.com/52235899/145375760-c6edf9ec-7ab4-4e38-9ead-4aeac66fac1e.mov



- **if the screen is in almost bottom area, it works.**

https://user-images.githubusercontent.com/52235899/145375685-aa1b0c81-35dd-4ce1-b880-d7e21fbf53e9.mov

## workaround
- add `jumpTo` before `animateTo` and wrap `animateTo` in `addPostFrameCallback`
```
          WidgetsBinding.instance?.scheduleFrameCallback(
            (_) => WidgetsBinding.instance?.addPostFrameCallback(
              (_) {
                _scrollController.jumpTo(
                  _scrollController.offset + Tolerance.defaultTolerance.distance,
                );
                WidgetsBinding.instance?.addPostFrameCallback(
                  (_) => _scrollController.animateTo(
                    _scrollController.position.maxScrollExtent,
                    duration: const Duration(milliseconds: 200),
                    curve: Curves.easeIn,
                  ),
                );
              },
            ),
          );
```

- or wrap it in while
```
 WidgetsBinding.instance?.scheduleFrameCallback(
                    (_) => WidgetsBinding.instance?.addPostFrameCallback(
                      (_) async {
                        while (!nearEqual(
                          _scrollController.position.maxScrollExtent,
                          _scrollController.offset,
                          Tolerance.defaultTolerance.distance,
                        )) {
                          await _scrollController.animateTo(
                            _scrollController.position.maxScrollExtent,
                            duration: const Duration(milliseconds: 200),
                            curve: Curves.linear,
                          );
                        }
                      },
                    ),
                  );
```