Home > Software design >  StreamProvider with Firebase RTDB
StreamProvider with Firebase RTDB

Time:01-25

I'm trying to utilize StreamProvider to see if there are changes in the friends of an user (i.e. if someone added/deleted the user). My current stream code is attached below. However I'm running into the issue A value of type 'Future<List<String?>>' can't be returned from the method 'getMyFriends' because it has a return type of 'Stream<List<String>>'

Essentially for this stream, I only want to monitor the keys (in this case the userIDs of the other users) and not the value of the friends so the stream should be returning a list of keys. Any suggestions is appreciated!

enter image description here

  Stream<List<String>> getMyFriends(String myUserID) {
    return rtdb
        .child('friends')
        .child(myUserID)
        .onValue
        .map((event) => event.snapshot)
        .map((data) => data.key).toList();
  }

CodePudding user response:

Use async* and yield* with Streams like so:

 Stream<List<String>> getMyFriends(String myUserID) async* {
    yield* rtdb
        .child('friends')
        .child(myUserID)
        .onValue
        .map((event){
          return event.snapshot.value.map((data) => data.key).toList();
        });
  }
  •  Tags:  
  • Related