Repository: flutter
Issue: Testing exceptions in onGenerateInitialRoutes throws unexpected "'_dependents.isEmpty': is not true"
Labels: a: tests,framework,f: routes,has reproducible steps,P2,found in release: 2.1,team-framework,triaged-framework

We're migrating from Flutter 1.22.5 to 2.0.2 and a test that throws exception inside a `onGenerateInitialRoutes` doesn't pass anymore.

The following code reproduces the problem:
```dart
testWidgets('test argument error', (tester) async {
      await tester.pumpWidget(
        MaterialApp(
          initialRoute: '/',
          routes: <String, WidgetBuilder>{
            '/': (context) => Container(),
          },
          onGenerateInitialRoutes: (initialRoute) {
            throw ArgumentError('some error');
          },
        ),
      );

      expect(tester.takeException(), isA<ArgumentError>()); // this assert works fine
    });
  ```
The error:
```
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown attaching to the render tree:
'package:flutter/src/widgets/framework.dart': Failed assertion: line 4954 pos 14:
'_dependents.isEmpty': is not true.

Either the assertion indicates an error in the framework itself, or we should provide substantially
more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=2_bug.md

When the exception was thrown, this was the stack:
#2      InheritedElement.debugDeactivated.<anonymous closure> (package:flutter/src/widgets/framework.dart:4954:14)
#3      InheritedElement.debugDeactivated (package:flutter/src/widgets/framework.dart:4956:6)
#4      _InactiveElements._deactivateRecursively.<anonymous closure> (package:flutter/src/widgets/framework.dart:2044:15)
#5      _InactiveElements._deactivateRecursively (package:flutter/src/widgets/framework.dart:2046:6)
#6      ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4549:14)
#7      _InactiveElements._deactivateRecursively (package:flutter/src/widgets/framework.dart:2042:13)
#8      ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4549:14)
#9      _InactiveElements._deactivateRecursively (package:flutter/src/widgets/framework.dart:2042:13)
#10     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4549:14)
#11     _InactiveElements._deactivateRecursively (package:flutter/src/widgets/framework.dart:2042:13)
#12     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4549:14)
#13     _InactiveElements._deactivateRecursively (package:flutter/src/widgets/framework.dart:2042:13)
#14     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4549:14)
#15     _InactiveElements._deactivateRecursively (package:flutter/src/widgets/framework.dart:2042:13)
#16     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4549:14)
#17     _InactiveElements._deactivateRecursively (package:flutter/src/widgets/framework.dart:2042:13)
#18     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4549:14)
#19     _InactiveElements._deactivateRecursively (package:flutter/src/widgets/framework.dart:2042:13)
#20     _InactiveElements.add (package:flutter/src/widgets/framework.dart:2054:7)
#21     Element.deactivateChild (package:flutter/src/widgets/framework.dart:3577:30)
#22     Element.updateChild (package:flutter/src/widgets/framework.dart:3301:9)
#23     RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1182:16)
#24     RenderObjectToWidgetElement.update (package:flutter/src/widgets/binding.dart:1160:5)
#25     RenderObjectToWidgetElement.performRebuild (package:flutter/src/widgets/binding.dart:1174:7)
#26     Element.rebuild (package:flutter/src/widgets/framework.dart:4189:5)
#27     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2694:33)
#28     AutomatedTestWidgetsFlutterBinding.drawFrame (package:flutter_test/src/binding.dart:1102:19)
#29     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:319:5)
#30     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
#31     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1082:9)
#32     AutomatedTestWidgetsFlutterBinding.scheduleWarmUpFrame (package:flutter_test/src/binding.dart:1048:5)
#33     runApp (package:flutter/src/widgets/binding.dart:1023:7)
#34     TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:797:7)
<asynchronous suspension>
(elided 2 frames from class _AssertionError)
════════════════════════════════════════════════════════════════════════════════════════════════════

Test failed. See exception logs above.
The test description was: test with error
```

Adding some extra error handling like in this [PR](https://github.com/flutter/flutter/pull/73082/files#diff-220b5f283ca6e5d4af33a8d0afd875f407f050a438e320f0766f38e4edf1f8c2R1021) solves the problem but I believe something is wrong in this behavior. So I'm reporting it.