| // Copyright 2013 The Flutter Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| import 'dart:async'; |
| |
| import 'package:flutter/foundation.dart'; |
| import 'package:flutter/material.dart'; |
| |
| import 'go_router.dart'; |
| |
| /// This class can be used to make `refreshListenable` react to events in the |
| /// the provided stream. This allows you to listen to stream based state |
| /// management solutions like for example BLoC. |
| /// |
| /// {@tool snippet} |
| /// Typical usage is as follows: |
| /// |
| /// ```dart |
| /// GoRouter( |
| /// refreshListenable: GoRouterRefreshStream(stream), |
| /// ); |
| /// ``` |
| /// {@end-tool} |
| class GoRouterRefreshStream extends ChangeNotifier { |
| /// Creates a [GoRouterRefreshStream]. |
| /// |
| /// Every time the [stream] receives an event the [GoRouter] will refresh its |
| /// current route. |
| GoRouterRefreshStream(Stream<dynamic> stream) { |
| notifyListeners(); |
| _subscription = stream.asBroadcastStream().listen( |
| (dynamic _) => notifyListeners(), |
| ); |
| } |
| |
| late final StreamSubscription<dynamic> _subscription; |
| |
| @override |
| void dispose() { |
| _subscription.cancel(); |
| super.dispose(); |
| } |
| } |