app_subscription_state.dart 637 B

123456789101112131415161718192021222324
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. mixin SubscriptionState<T extends StatefulWidget> on State<T> {
  4. final List<StreamSubscription<dynamic>> _streamSubscriptions = <StreamSubscription<dynamic>>[];
  5. void addSubscription(StreamSubscription<dynamic> streamSubscription) {
  6. _streamSubscriptions.add(streamSubscription);
  7. }
  8. @override
  9. void dispose() {
  10. disposeSubscription();
  11. super.dispose();
  12. }
  13. disposeSubscription() async {
  14. for (StreamSubscription<dynamic> subscription in _streamSubscriptions.reversed) {
  15. await subscription.cancel();
  16. }
  17. _streamSubscriptions.clear();
  18. }
  19. }