Last active
November 20, 2019 04:42
-
-
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 )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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