Tumin
Tumin

Reputation: 61

How do i modify the code to have a Stream<dynamic> type stream for stream builder?

Inside Stream builder, it is asking me to put Stream<builder> otherwise throwing an error,

type 'Future<dynamic>' is not a subtype of type 'Stream<dynamic>'

Now since I was following this answer, i had to code the stream something like this,

getGroupsOfUser() async {
    String _userID = await _getUID();
    DocumentSnapshot userInfo = await userCollection.document(_userID).get();
    return groupCollection.where(FieldPath.documentId, whereIn: userInfo.data['groups']).snapshots();
  }

Now I know maybe if I could overcome using async I may fix it but I need to get the uid and the array of groups which is an async function and I can't really assign them as variables otherwise I'm getting the error only static members can be accessed in initializers

Please, someone, help me I am very beginner in this.

Upvotes: 1

Views: 185

Answers (2)

Peter Haddad
Peter Haddad

Reputation: 80914

getGroupsOfUser() should return a Stream since snapshots() returns a Stream. Therefore you need to do the following:

Stream<QuerySnapshot> getGroupsOfUser() async* {
    String _userID = await _getUID();
    DocumentSnapshot userInfo = await userCollection.document(_userID).get();
    yield* groupCollection.where(FieldPath.documentId, whereIn: userInfo.data['groups']).snapshots();
  }

Since you need to return a stream then you need to use async* and use the yield* keyword to return.

Upvotes: 1

Rawaha Muhammad
Rawaha Muhammad

Reputation: 36

The error must be from the line

return groupCollection.where(FieldPath.documentId, whereIn: userInfo.data['groups']).snapshots();

Because .snapshots() method returns a Stream instead of a Future, and since your getGroupsOfUser() is a Future, it is throwing an error that you cannot return a Stream from a function that returns a Future.

My solution: 1 - Put the logic for getting the userID and the userInfo inside the initState() i.e make a Future for it. 2 - Seperate the last line from the Future and wrap it with a StreamBuilder after successfully getting the userinfo from step 1.

Upvotes: 1

Related Questions