0

I have a Future where users and posts are fetched from Firebase, turned into objects, added to either postsList or the usersList, and finally, the return is [usersList, postsList]. For so reason, though, the return looks like this when printed [[], []]. I think that the functions within are not completing before the return but I don't know how to fix that.

Here is the Future:

Future<List<QueryDocumentSnapshot>> getUserDocs() async {
    final userRef = FirebaseFirestore.instance.collection('users');
    final QuerySnapshot result = await userRef.get();
    final userDocs = result.docs;

    return Future.value(userDocs);
  }

  Future<List<QueryDocumentSnapshot>> getPostDocs() async {

    List<QueryDocumentSnapshot> userDocs = await getUserDocs();
    List<QueryDocumentSnapshot> postsDocs = [];

    for (final resultValue in userDocs) {
      final postsRef = FirebaseFirestore.instance
          .collection('users')
          .doc(resultValue.id)
          .collection('posts');
      final QuerySnapshot postsResult = await postsRef.get();
      final postDocs = postsResult.docs;

      print('postDocs: $postDocs');

      for (var post in postDocs) {
        postsDocs.add(post);
      }
    }
    return Future.value(postsDocs);
  }

  Future<List> fetchUsersAndPosts() async {

    List<QueryDocumentSnapshot> userDocs = await getUserDocs();
    List<QueryDocumentSnapshot> postsDocs = await getPostDocs();

    print('userDocs: $userDocs');
    print('postsDocs: $postsDocs');

    List<UserSearchResult> usersList = [];
    List<Post> postsList = [];

    for (var postsMap in postsDocs) {
      final post = Post.fromJson(postsMap.data() as Map<String, dynamic>);
      if (!postsList.contains(post)) {
        postsList.add(post);
      }
    }

    for (final value in userDocs) {
      final profileInfo =
      ProfileInfoObject.fromJson(value.data() as Map<String, dynamic>);

      Profile profile = Profile(
          profileInfo, postsList.where((p) => p.uid == value.id).toList());

      UserSearchResult user = (UserSearchResult(profile, value.id));

      if (usersList.where((u) => u.uid == user.uid).toList().isEmpty) {
        usersList.add(user);
        print('usersList: $usersList');
      }
    }

    postsList.sort((a, b) {
      return b.date.compareTo(a.date);
    });

    return [usersList, postsList];
  }
Globe
  • 514
  • 3
  • 17
  • Maybe your if conditions are not met so that nothing is being added to your list – Minjin Gelegdorj Sep 23 '22 at 03:39
  • All of the conditions should be met because I'm developing with a very small and controlled amount of data. The reason I think the loops aren't completed before returning is that the `print` parts inside them are never printed, not even blank ones. – Globe Sep 23 '22 at 03:52
  • Could you check if your postsMaps is not empty before looping? – Minjin Gelegdorj Sep 23 '22 at 04:05
  • I just checked... the `postDocs` and `userDocs` do seem to be empty. I tried to change some things (see updated code) but it still looks like the doc lists return empty even though they're there in Firebase. – Globe Sep 23 '22 at 04:12
  • `final userDocs = result.docs; print(userDocs.length);` what do you see if you add that `print` inside `getUserDocs` method? – pskink Sep 23 '22 at 05:09
  • Length prints out as 0... the expected length is 2. – Globe Sep 23 '22 at 05:15
  • if it is 0 it means that you have no data in 'users' collection - no wonder you receive `[[], []]` – pskink Sep 23 '22 at 07:26
  • That’s not true though. I have 2 documents in the users collection (and I can see them in the dashboard so I know they’re there) which is why expect the length to be 2. – Globe Sep 23 '22 at 17:14
  • Did you override the == function in class Post? – ming chen Sep 23 '22 at 17:26
  • Yes the Post class overrides == – Globe Sep 23 '22 at 19:31
  • So, I figured out one of the changes I made in the past must have fixed the original issue. Since I was building on web and there was an issue with my Firestore setup on web that's why everything was still empty and I didn't know I fixed it. – Globe Sep 24 '22 at 00:38

0 Answers0