简体   繁体   中英

A value of type 'Future<QuerySnapshot<Map<String, dynamic>>>' can't be assigned to a variable of type 'QuerySnapshot<Object?>'

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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM