Skip to content

Instantly share code, notes, and snippets.

@benoitjadinon
Last active November 20, 2019 04:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save benoitjadinon/5473b00bc416607aec24e6e84031ee3f to your computer and use it in GitHub Desktop.
Save benoitjadinon/5473b00bc416607aec24e6e84031ee3f to your computer and use it in GitHub Desktop.
BehaviorStreamBuilder, no more flicker because of initialData ( https://twitter.com/filiphracek/status/1050798900968181761 )
class BehaviorStreamBuilder<T> extends StreamBuilder<T>
{
BehaviorStreamBuilder({
Key key,
BehaviorSubject<T> stream,
@required AsyncWidgetBuilder<T> builder
}) : assert(builder != null),
super
(
key: key,
stream: stream.stream
.distinct() // next values won't redraw until different
.skip(1) // skips the first value from behavior, it's the same as initialData
,
initialData: stream.value,
builder: builder,
);
}
int _counter = 0;
var _bloc = MyBloc();
//
BehaviorStreamBuilder<String>(
stream: _bloc.letterSubject,
builder: (c, s) => Text("${s.data} #${(++_counter).toString()}"),
)
//
class MyBloc
{
var letterSubject = BehaviorSubject<String>.seeded("A");
MyBloc(){
// wont redraw cause value is the same
Observable.timer("A", Duration(seconds: 2)).listen(letterSubject.add);
// will redraw because new value
Observable.timer("B", Duration(seconds: 5)).listen(letterSubject.add);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment