1

I need to open the next widget after clicking the image which is received from the firebase database. I'm passing the postId of that image while navigating. This is my first widget from where I'm navigating to another widget.

Widget buildItem(BuildContext context, DocumentSnapshot document) {
    return GestureDetector(
        onLongPress: () {
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) =>
                      FullPhoto(url: document.data()['mediaUrl'])));
        },
        onTap: () {
          Navigator.push(context,
              MaterialPageRoute(builder: (context) => ShowPost(postId: document.data()['postId'])));
        },
        child: Container(
          margin: EdgeInsets.all(10),
          child: Image.network(
            document.data()['mediaUrl'],
            height: 300,
            width: MediaQuery.of(context).size.width,
            fit: BoxFit.cover,
          ),
        ));
  }
}

and this is the next activity where I'm navigating to.

class ShowPost extends StatefulWidget {


  String postId='';

  ShowPost({this.postId});

  
 @override
  PostState createState() => PostState(postId: this.postId);
}

class PostState extends State<ShowPost> {


    String postId='';

  PostState({this.postId,});


  String ownerId, username,user_Image, location, caption, mediaUrl;
  dynamic likes;
  int likesCount;
  bool isLiked;
  bool showHeart = false;
  

  void initState() {
    super.initState();
    getData();
  }

 getData()  {
   waitTillData();
}
Future<void> waitTillData()  async {
  await FirebaseFirestore.instance
    .collection('users')
    .doc(userUid)
    .collection('Posts')
        .doc(postId)
        .get()
    .then((DocumentSnapshot documentSnapshot) {
      if (documentSnapshot.exists) {
        ownerId= documentSnapshot.data()['ownerId'];
      username= documentSnapshot.data()['username'];
      mediaUrl= documentSnapshot.data()['mediaUrl'];
      caption=documentSnapshot.data()['caption'];
      likes = documentSnapshot.data()['likes'];  
    
    setState(() {
      load=true;
   });
      } else {
        print('Document does not exist on the database');
      }
    });   
}

What could be the possible solution?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 2
    Can you add the stack trace of the error? Actually, in your code, there's no reference on myUserId so I couldn't understand where the error was thrown. It's probably related to an empty document, but I need a clearer vision to say so – AndreaCostanzo1 Nov 29 '20 at 12:23

0 Answers0