0
     @override
     Widget build(BuildContext context) {
       return FutureBuilder(
         future: postsRef
             .document(userId)
             .collection("usersPosts")
             .document(postId)
             .get(),
         builder: (context, snapshot) {
           if (!snapshot.hasData) {
             return circularProgress();
           }
           Post post = Post.fromDocument(snapshot.data);
           return Center(
             child: Scaffold(
               appBar: header(context, titleText: post.description),
               body: ListView(
                 children: [
                   Container(
                     child: post,
                   )
                 ],
               ),
             ),
           );
         },
       );
     }

also this my error

The following NoSuchMethodError was thrown building FutureBuilder<DocumentSnapshot>(dirty, state: _FutureBuilderState<DocumentSnapshot>#37f30):
The method '[]' was called on null.
Receiver: null
Tried calling: []("postId")

The relevant error-causing widget was: 
  FutureBuilder<DocumentSnapshot> file:///D:/Flutter/KadShare/kadshare/lib/pages/post_screen.dart:18:12
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1      DocumentSnapshot.[] (package:cloud_firestore/src/document_snapshot.dart:29:42)
#2      new Post.fromDocument (package:kadshare/widgets/post.dart:34:18)
#3      PostScreen.build.<anonymous closure> (package:kadshare/pages/post_screen.dart:28:26)
#4      _FutureBuilderState.build (package:flutter/src/widgets/async.dart:773:55)

My Post Class

      final String postId;
      final String ownerId;
      final String username;
      final String location;
      final String description;
      final String mediaUrl;
      final dynamic likes;
    
      Post({
        this.postId,
        this.ownerId,
        this.username,
        this.location,
        this.description,
        this.mediaUrl,
        this.likes,
      });
    
      factory Post.fromDocument(DocumentSnapshot doc) {
        return Post(
          postId: doc["postId"],
          ownerId: doc["ownerId"],
          username: doc["username"],
          location: doc["location"],
          description: doc["description"],
          mediaUrl: doc["mediaUrl"],
          likes: doc["likes"],
        );
      }
Adelina
  • 10,915
  • 1
  • 38
  • 46

3 Answers3

0

Your snapshot.data is null;

Maybe change:

        if (!snapshot.hasData || snapshot.data == null) {

Adelina
  • 10,915
  • 1
  • 38
  • 46
  • You should be passing null value in here > `Post.fromDocument(snapshot.data)`; so if you are checking beforehand, it should not happen... not sure what else it could be – Adelina Apr 20 '21 at 20:08
  • I thought maybe postId it's not same with firebase i checked but it's corret. thanks anyway – Mustafa Kadak Apr 20 '21 at 20:11
0

Your snapshot is a Flutter AsyncSnapshot, specifically a AsyncSnapshot<DocumentSnapshot>. When snapshot.hasData is true, that means the DocumentSnapshot exists.

But a DocumentSnapshot exists even when the underlying document doesn't exist in the database, so you also need to check if the DocumentSnapshot has data, which you do with DocumentSnapshot.exists.

So your check then becomes:

if (!snapshot.hasData && snapshot.data.exists) {

So this change means the spinner will keep being rendered if the document doesn't exist.

Alternatively, you may want to render a different UI if the document doesn't exist:

 builder: (context, snapshot) {
   if (!snapshot.hasData) {
     return circularProgress();
   }
   if (!snapshot.data.exists) {
     return Text("Document does not exist");
   }
   Post post = Post.fromDocument(snapshot.data);
   return Center(
     child: Scaffold(
       appBar: header(context, titleText: post.description),
       body: ListView(
         children: [
           Container(
             child: post,
           )
         ],
       ),
     ),
   );
 },

Also see What is the difference between existing types of snapshots in Firebase?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

i assume you are working on fluttershare course app, in your activity feed page when you press show post function make sure you give userId your currentUser.id passing in a non-null value for the 'postId' parameter as shown here:

  showPost(context) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => PostScreen(
          userId: currentUser.id,
          postId: postId,
        ),
      ),
    );
  }
NAEEM
  • 1
  • 2