Repository: flutter
Issue: Navigator 2: Screen not update when I delete the last x pages from Navigator.pages
Labels: framework,f: routes,has reproducible steps,P2,found in release: 2.10,found in release: 2.13,team-framework,triaged-framework

## Steps to Reproduce

1. Run `flutter create bug`.
2. Update main as follows: 
  <details>
    <summary>Navigato 2 Code</summary>
    
```dart
  import 'package:flutter/material.dart';
  
  void main() {
    runApp(MyApp());
  }
  
  class AppState extends ChangeNotifier {
    final List<int> _indexs = List.generate(5, (index) => index);
  
    int _selectedIndex = 0;
  
    int get selectedIndex => _selectedIndex;
  
    set selectedIndex(int id) {
      _selectedIndex = id;
      notifyListeners();
    }
  
    List<int> get indexs => _indexs;
  }
  
  class MyApp extends StatefulWidget {
    @override
    State<StatefulWidget> createState() => _MyAppState();
  }
  
  class _MyAppState extends State<MyApp> {
    final IndexRouterDelegate _routerDelegate = IndexRouterDelegate();
    final IndexRouteInformationParser _routeInformationParser =
        IndexRouteInformationParser();
  
    @override
    void dispose() {
      _routerDelegate.dispose();
      super.dispose();
    }
  
    @override
    Widget build(BuildContext context) {
      return MaterialApp.router(
        title: 'Index App',
        routerDelegate: _routerDelegate,
        routeInformationParser: _routeInformationParser,
      );
    }
  }
  
  class IndexRouteInformationParser extends RouteInformationParser<int> {
    @override
    Future<int> parseRouteInformation(RouteInformation routeInformation) async {
      if (routeInformation.location == '/') {
        return 0;
      }
      final uri = Uri.parse(routeInformation.location!);
      final id = int.tryParse(uri.pathSegments[0]);
      return id!;
    }
  
    @override
    RouteInformation restoreRouteInformation(int path) =>
        RouteInformation(location: '/$path');
  }
  
  class IndexRouterDelegate extends RouterDelegate<int>
      with ChangeNotifier, PopNavigatorRouterDelegateMixin<int> {
    @override
    final GlobalKey<NavigatorState> navigatorKey;
    final AppState _appState = AppState();
  
    IndexRouterDelegate() : navigatorKey = GlobalKey<NavigatorState>() {
      _appState.addListener(() => notifyListeners());
    }
  
    @override
    void dispose() {
      _appState.dispose();
      super.dispose();
    }
  
    @override
    int get currentConfiguration => _appState.selectedIndex;
  
    List<Page> get getPages {
      final result = <Page>[];
      for (var i = 0; i <= _appState.selectedIndex; i++)
        result.add(IndexPage(i: _appState.indexs[i], state: _appState));
      print('Pages count : ${result.length}');
      return List.unmodifiable(result);
    }
  
    @override
    Widget build(BuildContext context) {
      return Navigator(
        key: navigatorKey,
        pages: getPages,
        onPopPage: (route, result) {
          if (!route.didPop(result)) {
            return false;
          }
          return true;
        },
      );
    }
  
    @override
    Future<void> setNewRoutePath(int path) async {
      _appState.selectedIndex = path;
    }
  }
  
  class IndexPage extends Page<dynamic> {
    final int i;
    final AppState state;
  
    IndexPage({
      required this.i,
      required this.state,
    }) : super(key: ValueKey(i));
  
    @override
    Route<dynamic> createRoute(BuildContext context) {
      return MaterialPageRoute(
        settings: this,
        builder: (BuildContext context) {
          return IndexScreen(
            index: i,
            state: state,
          );
        },
      );
    }
  }
  
  class IndexScreen extends StatelessWidget {
    final int index;
    final AppState state;
    IndexScreen({
      required this.state,
      required this.index,
    });
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(),
        drawer: Drawer(
          child: ListView(
            children: [
              for (var i = 0; i < state.indexs.length; i++)
                ListTile(
                  title: Text("$i"),
                  onTap: () => state.selectedIndex = i,
                )
            ],
          ),
        ),
        body: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Center(
              child:
                  Text('$index', style: Theme.of(context).textTheme.headline6)),
        ),
      );
    }
  }
  
```
    ```
  </details>
3.  Follow these steps

   - Run the app
   - Open Drawer
   - Go to page 1
   - Go to page 2
   - Open Drawer and go back to page 0 or 1


**Expected results:** Page 0 or 1 is shown

**Actual results:** Page 2 is shown (although it is not in Navigator.page) 
Check the logs when you are navigating, the pages.length will be logged

<details>
  <summary>Logs</summary>

## flutter doctor -v

[√] Flutter (Channel dev, 2.3.0-1.0.pre, on Microsoft Windows [Version 10.0.19042.928], locale en-US)
    • Flutter version 2.3.0-1.0.pre at C:\Tools\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision d97f41caed (13 days ago), 2021-04-30 12:35:21 -0700
    • Engine revision e7939e091e
    • Dart version 2.14.0 (build 2.14.0-48.0.dev)

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at C:\Users\Schab\AppData\Local\Android\sdk
    • Platform android-30, build-tools 30.0.3
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • CHROME_EXECUTABLE = C:\Users\Schab\xmlhttperror.cmd

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.9.4)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.9.31205.134
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 4.1.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    • 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 1.8.0_242-release-1644-b01)

[√] VS Code (version 1.56.1)
    • VS Code at C:\Users\Schab\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.22.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19042.928]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 90.0.4430.212
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 90.0.818.49

• No issues found!

</details>


https://user-images.githubusercontent.com/49782771/118126030-0447ae80-b3f8-11eb-8f2b-f0219b2b6f78.mp4