Steps to Reproduce
I am trying to use the bloc pattern with firebase and having hard time since there is not good tutorial out there which uses firebase
and bloc pattern
.
I've faced strange thing while doing this and worked hard to find out the problem but couldn't.
I've created UserBloc
and tried to set UserModel
after listening to Firestore snapshot
.
class UserBloc extends Object {
final _user = StreamController<UserModel>.broadcast();
final _uid = StreamController<String>.broadcast();
Stream<UserModel> get user => _user.stream;
Stream<String> get uid => _uid.stream;
Function(UserModel) get setUser => _user.sink.add;
Function(String) get setUID => _uid.sink.add;
Stream<DocumentSnapshot> userStream;
UserBloc() {
uid.listen((uid) {
print('user : $uid');
if (uid != null) {
fromUID(uid);
}
});
}
fromUID(String uid) {
print('fromUID');
userStream = Firestore.instance.collection('users').document(uid).snapshots();
/// works ok when I delete below subscription
userStream.listen((data) {
});
}
dispose() {
_user.close();
_uid.close();
}
}
Then I used the StreamBuilder
inside my widget like below.
StreamBuilder(
stream: userBloc.userStream,
builder: (context, snapshot) {
print(snapshot.connectionState);
print(snapshot.data);
return Text('testing');
},
);
The problem is the snapshot.connectionState**
is always **waiting
. However, when I delete the subscription code which is userStream.listen((data) {
, I was able to get this working. All the example do the listen and set the data in the constructor of bloc
. I have no idea why this is happening.