I'm developing a flutter app and using firestore as backend: my DB structure is:
- User (Collection)
- userid (User property)
- Books (SubCollection)
- booksList (array of Objects(or map as firestore calls them))
The function to retrieve each book from booksList array is:
Future<List<Book>> bookshelf(String userId) async {
return await FirebaseFirestore.instance
.collection('Users')
.where('userId', isEqualTo: userId)
.get()
.then((querySnapshot) => querySnapshot.docs.map((doc) => doc.reference
.collection('Books')
.get()
.then((querySnapshot) => querySnapshot.docs.forEach((doc) {
var books = doc.data()['bookList'];
return books
.map<Book>((elem) => Book(title: elem['title']));
}))).toList());
My problem is that I'm not able to return Future<List<Book>>;
I've tried to create an array and add elements to it at line 12 (where I'm actually getting the books correctly) but it didn't work since the return statement wasn't waiting for the query to complete.
Basically I'm not able to transform the object I get into a Future<List> as I need for the function.
Right now I'm getting a "MappedListIterable<QueryDocumentSnapshot, Future>".
Thanks.