Skip to content

Instantly share code, notes, and snippets.

@budius
Last active March 10, 2020 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save budius/b99a252f0f0c819bcf6391077adfbc72 to your computer and use it in GitHub Desktop.
Save budius/b99a252f0f0c819bcf6391077adfbc72 to your computer and use it in GitHub Desktop.
A Widget that subscribes/cancels to a Stream and dispatch the events to the supplied listen function.
/*
MIT License
Copyright (c) 2020 Ronaldo Pace
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import 'dart:async';
import 'package:flutter/widgets.dart';
/// A widget that subscribes/cancels to a Stream
/// and dispatch the events to the supplied [listen] function.
///
/// Different from StreamBuilder,
/// this widget does not re-built when a stream event happen.
///
/// This is useful for events coming from the block
/// that does not change the UI state of the widget,
///
/// Example, Navigation:
///
/// return StreamListener<String>(
/// child: myUi,
/// stream: bloc.navigation
/// listen: (data) { Navigator.pushNamed(context, data) }
/// );
class StreamListener<T> extends StatefulWidget {
const StreamListener({
Key key,
@required this.child,
@required this.stream,
@required this.listen,
}) : super(key: key);
final Widget child;
final Stream<T> stream;
final void Function(T) listen;
@override
State<StatefulWidget> createState() {
return _StreamListenerState<T>(child, stream, listen);
}
}
class _StreamListenerState<T> extends State<StreamListener> {
_StreamListenerState(this._child, this._stream, this._listen);
Widget _child;
Stream<T> _stream;
void Function(T) _listen;
StreamSubscription<T> _subscription;
@override
void initState() {
_subscribe();
super.initState();
}
void _subscribe() {
_subscription?.cancel();
_subscription = _stream.listen(_listen);
}
void _dispose() {
_subscription?.cancel();
_subscription = null;
}
@override
void didUpdateWidget(StreamListener<T> oldWidget) {
super.didUpdateWidget(oldWidget);
_dispose();
_child = widget.child;
_stream = widget.stream;
_listen = widget.listen;
_subscribe();
}
@override
void dispose() {
_dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return _child;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment