4
2
Fork 0

fix: reimplemented UserAvatar bloc usage to avoid making network calls inside build statements

pull/45/head
Sudharshan S. 2019-03-24 09:13:51 +08:00
parent 812843ce92
commit d0ba21e202
Signed by: sudharshan
GPG Key ID: C861C97AAF3D9559
3 changed files with 13 additions and 6 deletions

View File

@ -17,7 +17,7 @@ class Routes {
title: "Beep",
theme: theme,
routes: routes,
home: Home(),
home: Welcome(),
));
}
}

View File

@ -26,6 +26,7 @@ class HeartbeatReceiverBloc {
status = "";
loginManager.getToken().then((token) {
print(token);
EventSource.connect("$baseUrlHeartbeat/subscribe/$userId?token=$token")
.then((es) {
es.listen((Event event) {

View File

@ -7,13 +7,11 @@ class UserAvatar extends StatefulWidget {
final User user;
final EdgeInsetsGeometry padding;
final double radius;
final bloc;
UserAvatar(
{@required this.user,
this.padding: const EdgeInsets.all(0.0),
this.radius: 20.0})
: bloc = HeartbeatReceiverBloc(user.id);
this.radius: 20.0});
@override
State<StatefulWidget> createState() {
@ -22,9 +20,17 @@ class UserAvatar extends StatefulWidget {
}
class _UserAvatarState extends State<UserAvatar> {
HeartbeatReceiverBloc bloc;
@override
void initState() {
super.initState();
bloc = HeartbeatReceiverBloc(widget.user.id);
}
@override
void dispose() {
widget.bloc.dispose();
bloc.dispose();
super.dispose();
}
@ -42,7 +48,7 @@ class _UserAvatarState extends State<UserAvatar> {
),
radius: widget.radius),
StreamBuilder(
stream: widget.bloc.colours,
stream: bloc.colours,
builder: (context, AsyncSnapshot<Color> snapshot) {
Color colour = Color.fromARGB(255, 158, 158, 158);
if (snapshot.hasData) {