Repository: flutter
Issue: Throwing in didChangeDependencies after establishing dependencies causes failures during test teardown
Labels: a: tests,c: crash,framework,has reproducible steps,P2,found in release: 3.3,found in release: 3.7,team-framework,triaged-framework

```dart
// @dart = 2.8

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('example', (WidgetTester tester) async {
    await tester.pumpWidget(Foo(child: Bar()));
    expect(tester.takeException(), isFlutterError);

// Work around:
//    await tester.pumpWidget(Container());
//    expect(tester.takeException(), isAssertionError);
  });
}

class Foo extends InheritedWidget {
  const Foo({Widget child}) : super(child: child);

  static Foo of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<Foo>();
  }

  @override
  bool updateShouldNotify(covariant InheritedWidget oldWidget) => false;
}

class Bar extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => BarState();
}

class BarState extends State<Bar> {
  Foo currentFoo;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    currentFoo = Foo.of(context);
    throw FlutterError('something went wrong');
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

```


```
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown attaching to the render tree:
'package:flutter/src/widgets/framework.dart': Failed assertion: line 5100 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=BUG.md

When the exception was thrown, this was the stack:
#2      InheritedElement.debugDeactivated.<anonymous closure> (package:flutter/src/widgets/framework.dart:5100:14)
#3      InheritedElement.debugDeactivated (package:flutter/src/widgets/framework.dart:5102:6)
#4      _InactiveElements._deactivateRecursively.<anonymous closure> (package:flutter/src/widgets/framework.dart:2020:15)
#5      _InactiveElements._deactivateRecursively (package:flutter/src/widgets/framework.dart:2022:6)
#6      _InactiveElements.add (package:flutter/src/widgets/framework.dart:2030:7)
#7      Element.deactivateChild (package:flutter/src/widgets/framework.dart:3605:29)
#8      Element.updateChild (package:flutter/src/widgets/framework.dart:3322:9)
#9      RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1252:16)
#10     RenderObjectToWidgetElement.update (package:flutter/src/widgets/binding.dart:1230:5)
#11     RenderObjectToWidgetElement.performRebuild (package:flutter/src/widgets/binding.dart:1244:7)
#12     Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5)
#13     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2730:33)
#14     AutomatedTestWidgetsFlutterBinding.drawFrame (package:flutter_test/src/binding.dart:1088:18)
#15     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:302:5)
#16     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1117:15)
#17     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1055:9)
#18     AutomatedTestWidgetsFlutterBinding.scheduleWarmUpFrame (package:flutter_test/src/binding.dart:1036:5)
#19     runApp (package:flutter/src/widgets/binding.dart:1063:7)
#20     TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:791:7)
<asynchronous suspension>
#23     TestWidgetsFlutterBinding._runTest (package:flutter_test/src/binding.dart:764:14)
#24     AutomatedTestWidgetsFlutterBinding.runTest.<anonymous closure> (package:flutter_test/src/binding.dart:1173:24)
#25     FakeAsync.run.<anonymous closure>.<anonymous closure> (package:fake_async/fake_async.dart:178:54)
#30     withClock (package:clock/src/default.dart:48:10)
#31     FakeAsync.run.<anonymous closure> (package:fake_async/fake_async.dart:178:22)
#36     FakeAsync.run (package:fake_async/fake_async.dart:178:7)
#37     AutomatedTestWidgetsFlutterBinding.runTest (package:flutter_test/src/binding.dart:1170:15)
#38     testWidgets.<anonymous closure> (package:flutter_test/src/widget_tester.dart:138:24)
#39     Declarer.test.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/declarer.dart:175:19)
<asynchronous suspension>
#40     Declarer.test.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/declarer.dart)
#45     Declarer.test.<anonymous closure> (package:test_api/src/backend/declarer.dart:173:13)
#46     Invoker.waitForOutstandingCallbacks.<anonymous closure> (package:test_api/src/backend/invoker.dart:231:15)
#51     Invoker.waitForOutstandingCallbacks (package:test_api/src/backend/invoker.dart:228:5)
#52     Invoker._onRun.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/invoker.dart:383:17)
<asynchronous suspension>
#53     Invoker._onRun.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/invoker.dart)
#58     Invoker._onRun.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/invoker.dart:370:9)
#59     Invoker._guardIfGuarded (package:test_api/src/backend/invoker.dart:415:15)
#60     Invoker._onRun.<anonymous closure> (package:test_api/src/backend/invoker.dart:369:7)
#67     Invoker._onRun (package:test_api/src/backend/invoker.dart:368:11)
#68     LiveTestController.run (package:test_api/src/backend/live_test_controller.dart:153:11)
#69     RemoteListener._runLiveTest.<anonymous closure> (package:test_api/src/remote_listener.dart:256:16)
#74     RemoteListener._runLiveTest (package:test_api/src/remote_listener.dart:255:5)
#75     RemoteListener._serializeTest.<anonymous closure> (package:test_api/src/remote_listener.dart:208:7)
#93     _GuaranteeSink.add (package:stream_channel/src/guarantee_channel.dart:125:12)
#94     new _MultiChannel.<anonymous closure> (package:stream_channel/src/multi_channel.dart:159:31)
#98     CastStreamSubscription._onData (dart:_internal/async_cast.dart:85:11)
#132    new _WebSocketImpl._fromSocket.<anonymous closure> (dart:_http/websocket_impl.dart:1145:21)
#140    _WebSocketProtocolTransformer._messageFrameEnd (dart:_http/websocket_impl.dart:338:23)
#141    _WebSocketProtocolTransformer.add (dart:_http/websocket_impl.dart:232:46)
#151    _Socket._onData (dart:io-patch/socket_patch.dart:2150:41)
#160    new _RawSocket.<anonymous closure> (dart:io-patch/socket_patch.dart:1686:33)
#161    _NativeSocket.issueReadEvent.issue (dart:io-patch/socket_patch.dart:1182:14)
(elided 113 frames from class _AssertionError, dart:async, and package:stack_trace)
```