Home > Net >  A value of type 'Future<QuerySnapshot<Map<String, dynamic>>>' can't
A value of type 'Future<QuerySnapshot<Map<String, dynamic>>>' can't

Time:09-05

I'm creating a social media type-app and this function creates a post publicly for everyone on the app to see

static void createPost(Post post) {
    postsRef.doc(post.authorId).set({'postTime': post.timestamp});
    postsRef.doc(post.authorId).collection("userPosts").add({
      "text": post.text,
      "image": post.image,
      "authorId": post.authorId,
      "timestamp": post.timestamp,
      "likes": post.likes
    }).then((doc) {
      QuerySnapshot homeSnapshot =
          usersRef.doc(post.authorId).collection('users').get();

      for (var docSnapshot in homeSnapshot.docs) {
        feedRefs.doc(docSnapshot.id).collection('userFeed').doc(doc.id).set({
          "text": post.text,
          "image": post.image,
          "authorId": post.authorId,
          "timestamp": post.timestamp,
          "likes": post.likes
        });
      }
    });
  }

the error part of this is the

QuerySnapshot homeSnapshot =
          usersRef.doc(post.authorId).collection('users').get();

then also the Post model being passed in the function if needed:

import 'package:cloud_firestore/cloud_firestore.dart';

class Post {
  String id;
  String authorId;
  String text;
  String image;
  Timestamp timestamp;
  int likes;

  Post({
    required this.id,
    required this.authorId,
    required this.text,
    required this.image,
    required this.timestamp,
    required this.likes,
  });

  factory Post.fromDoc(DocumentSnapshot doc) {
    return Post(
      id: doc.id,
      authorId: doc['authorId'],
      text: doc['text'],
      image: doc['image'],
      timestamp: doc['timestamp'],
      likes: doc['likes'],
    );
  }
}

then obviously the error is in the title, any help is much appreciated, thank you :D

CodePudding user response:

You should await for async function. So change this:

QuerySnapshot homeSnapshot =
          usersRef.doc(post.authorId).collection('users').get();

to this:

QuerySnapshot homeSnapshot =
              await usersRef.doc(post.authorId).collection('users').get();
  • Related