0
votes

Within my firestore db I have the directory users which has user data as documents. The document is having two fields, String 'displayName' and Array 'friends'. I am struggling to retrieve the friends array and get it into a list in flutter within a stream. Any help is appreciated.

My code as it currently stands is as follows:

Stream<List<Memo>> get memos {
    var userdocs = Firestore.instance.collection('users').document(userid).document('friends');
    // Complete request and do list conversions 

    // Do more work using results
}

I recognize that this question has been asked before but I am very new to flutter and asynchronous programming do not completely understand the answers

1
Firestore paths needs to follow this rules 'collection/document/collection/document' so if the friends are a collection of the document user you have to call .collection after document(userId), this way 'document(userId).collection('friends'), if you call document again will not works. - Jorge Vieira
friends is a document of type String Array. In the past it has been a collection with each document representing a friend but I was unable to retrieve a list of document names (the friend names) so I switched it to this format though the previous one was preferred - FANCY_HOOMAN
Why you couldn't get the friends list? it should be possible and you could use a query to filter. If you use an array inside the document you will have the array here " Firestore.instance.collection('users').document(userid)", because the array is part of the document 'userId', you just need to find a way to extract the data from it, but if the friends list grows much maybe is not a good idea use an array cause can extrapolate the document limit size and a collection can grows forever. - Jorge Vieira
He is unable to clarify what he wants quite correctly. I have suggested an edit. Let him see & accept that. - Yogesh Aggarwal
Thank you for the suggestion Yogesh, I have accepted it. - FANCY_HOOMAN

1 Answers

1
votes

The flutter SDK returns a Stream object which will emmit the values over time (realtime database). In your example, you are assigning a Stream value & expecting a List. In asynchronous programming you have to subscribe to an event so that whenever a value emitted, we can perform our desired operation.

var userdocs
Firestore.instance
    .collection("users")
    .document(userid)
    .get()
    .then((DocumentSnapshot ds) {
       userdocs = ds.data
     })

Or use await if you are inside an async function

var userdocs = await Firestore.instance
    .collection("users")
    .document(userid)
    .get()