Repository: flutter
Issue: StreamBuilder calls `setState()` after `dispose()`
Labels: framework,has reproducible steps,P3,found in release: 2.5,found in release: 2.6,team-framework,triaged-framework

Certain `StreamSubscriptions` may call the `onDone` callback after `cancel` has been called. I am not sure if this is a bug in dart or if this is actually allowed (the documentation of `StreamSubscription.cancel` says "After this call, the subscription no longer receives events.", but does that mean as soon as `cancel` is called or only after the future it returns is completed). 
Anyway, as a result, the `setState` on a `StreamBuilder`, might get called after  `dispose` is called.


## Steps to Reproduce

A broadcast stream controller that is already closed or a stream on which you call `.take(0)` will return a `_DoneStreamSubscription`. This stream subscription might call the `onDone` callback even after `cancel` is called. Not sure if there are maybe other types of stream subscriptions with the same behavior.

<details>
<summary>Code sample</summary>

This code reproduces the issue

```dart
void main() async {
  var value = ValueNotifier(true);

  runZoned(() {
    runApp(ValueListenableBuilder(
        valueListenable: value,
        builder: (context, v, child) {
          if (v) {
            return StreamBuilder(
              stream: Stream.value(null).take(0),
              builder: (context, snapshot) {
                SchedulerBinding.instance.addPostFrameCallback((v) {
                  value.value = false;
                });
                return Container(color: Colors.red);
              },
            );
          }
          return Container(color: Colors.blue);
        }));
  }, zoneSpecification:
      ZoneSpecification(scheduleMicrotask: (self, parent, zone, f) {
    if (StackTrace.current.toString().contains('_DoneStreamSubscription')) {
      // as the problem only happens when microtasks are executed in a specific order, we similate it here by delaying the execution of the onDone callback
      Future.delayed(Duration(milliseconds: 1000)).then((_) { 
        parent.scheduleMicrotask(zone, f);
      });
    } else {
      parent.scheduleMicrotask(zone, f);
    }
  }));
}
```

</details>

<details>
  <summary>Logs</summary>

This throws the following error:

```
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: setState() called after dispose(): _StreamBuilderBaseState<Null, AsyncSnapshot<Null>>#4aa5c(lifecycle state: defunct, not mounted)
This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.
The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().
#0      State.setState.<anonymous closure>
package:flutter/…/widgets/framework.dart:1052
#1      State.setState
package:flutter/…/widgets/framework.dart:1087
#2      _StreamBuilderBaseState._subscribe.<anonymous closure>
package:flutter/…/widgets/async.dart:143
#3      _rootRun (dart:async/zone.dart:1420:47)
#4      _CustomZone.run (dart:async/zone.dart:1328:19)
#5      _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
#6      _DoneStreamSubscription._sendDone (dart:async/stream_impl.dart:785:36)
#7      _rootRun (dart:async/zone.dart:1428:13)
#8      _CustomZone.run (dart:async/zone.dart:1328:19)
#9      _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
#10     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
#11     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#12     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
````

Output of `flutter doctor -v`

```
[✓] Flutter (Channel stable, 2.5.2, on macOS 11.5.2 20G95 darwin-arm, locale nl-BE)
    • Flutter version 2.5.2 at /Users/rikbellens/tools/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 3595343e20 (2 weeks ago), 2021-09-30 12:58:18 -0700
    • Engine revision 6ac856380f
    • Dart version 2.14.3

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Users/rikbellens/Library/Android/sdk
    • Platform android-30, build-tools 30.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 13.0, Build version 13A233
    • CocoaPods version 1.10.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.1)
    • 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 1.8.0_242-release-1644-b3-6915495)

[✓] VS Code (version 1.52.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.20.0

[✓] VS Code (version 1.62.0-insider)
    • VS Code at /Applications/Visual Studio Code - Insiders.app/Contents
    • Flutter extension version 3.27.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 11.5.2 20G95 darwin-arm
    • Chrome (web)    • chrome • web-javascript • Google Chrome 94.0.4606.81

• No issues found!
```

</details>