I’m facing a error The method ‘[]’ was called on null. Receiver: null Tried calling: []("postId")

  android, firebase, flutter, methods
     @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"],
        );
      }

Source: Android Questions

LEAVE A COMMENT