Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
63 lines (51 sloc) 1.96 KB
import 'dart:async';
import 'package:flutter/widgets.dart' as flutter;
import 'package:meta/meta.dart';
/// Widget builder function to be called when the listenable updates.
/// Passes the [BuildContext] and current [Listenable].
typedef flutter.Widget ReactiveWidgetBuilder<T extends flutter.Listenable>(flutter.BuildContext context, T model);
/// Widget that rebuilds when the specified [Listenable] updates.
/// Optionally typed, but the listenable you past must inherit [Listenable].
/// Internally creates a stream of [Listenable] updates by listening to the passed
/// [Listenable], and builds a [StreamBuilder] widget which consumes this stream.
class ReactiveWidget<T extends flutter.Listenable> extends flutter.StatefulWidget {
ReactiveWidget({@required, @required this.builder})
: assert(watch != null),
assert(builder != null);
/// The model to react to
final T watch;
/// Builder that gets passed the context and the current version of the listenable
final ReactiveWidgetBuilder<T> builder;
createState() => _ReactiveWidgetState<T>();
class _ReactiveWidgetState<T extends flutter.Listenable> extends flutter.State<ReactiveWidget<T>> {
// sink gets closed in dispose, bad warning suppressed:
// ignore: close_sinks
StreamController<T> controller;
void onUpdate() {
initState() {
this.controller = StreamController<T>();;
dispose() {;
build(context) => flutter.StreamBuilder<T>(
builder: (context, snap) => snap.hasData
? this.widget.builder(context,
: flutter.Container() // should never happen, since we pass initialData
You can’t perform that action at this time.