Reputation: 3545
I have a Flutter app which uses Firebase-storage and google-signin. the steps I am trying to do is so simple:
1- Sign-in using Google (Done).
2- Get Current User Id (Done).
3- Use the User Id when construct the stream for the stream builder (the problem).
what I did so far is that I am using a Future
to get the Current User Id,
then to inject the user Id inside the Where clause
.where('userId', isEqualTo: userId)
and this is what I end up with:
this is the part where I should create the stream:
// Get document's snapshots and return it as stream.
Future<Stream> getDataStreamSnapshots() async {
// Get current user.
final User user = await FirebaseAuth().currentUser();
String userId = user.uid;
Stream<QuerySnapshot> snapshots =
db
.collection(db)
.where("uid", isEqualTo: userId)
.snapshots();
try {
return snapshots;
} catch(e) {
print(e);
return null;
}
}
and this is the part where should I call and receive the stream,
...
children: <Widget>[
StreamBuilder<QuerySnapshot>(
stream: CALLING THE PREVIOUS FUNCTION,
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasData) {
...
}
...
But this code does not work, because I am not able to get the value that should returned by the Future? any idea?
thanks a lot
Upvotes: 7
Views: 4737
Reputation: 71623
You should never have a Future<Stream>
, that's double-asynchrony, which is unnecessary. Just return a Stream
, and then you don't have to emit any events until you are ready to.
It's not clear what the try
/catch
is guarding because a return of a non-Future
cannot throw. If you return a stream, just emit any error on the stream as well.
You can rewrite the code as:
Stream<QuerySnapshot> getDataStreamSnapshots() async* {
// Get current user.
final User user = await FirebaseAuth().currentUser();
String userId = user.uid;
yield* db
.collection(db)
.where("uid", isEqualTo: userId)
.snapshots();
}
An async*
function is asynchronous, so you can use await
. It returns a Stream
, and you emit events on the stream using yield event;
or yield* streamOfEvents;
.
Upvotes: 16