Ankan Sharma
Ankan Sharma

Reputation: 61

Documents in Cloud Firestore is not showing up in query result in flutter

Detail Cloud firestore structureScreenshot of Cloud Firestore

Code to retrieve document from Cloud Firestore

More Detail

  void onSendMessage(String content) {
    
    if (content.trim() != '') {
      textEditingController.clear();
      var documentReference = FirebaseFirestore.instance
          .collection('messages')
          .doc(widget.comid)
          .collection(widget.comid)
          .doc(DateTime.now().millisecondsSinceEpoch.toString());

      FirebaseFirestore.instance.runTransaction((transaction) async {
        transaction.set(
          documentReference,
          {
            'idFrom': widget.userid,
            'idTo': widget.peerid,
            'timestamp': DateTime.now().millisecondsSinceEpoch.toString(),
            'content': content,
            
          },
        );
      });
    }
    else {
      Fluttertoast.showToast(
        msg: 'Nothing to send', 
        backgroundColor: Colors.black, 
        textColor: Colors.grey[100]
      );
    }
  }

I want to get those document id but its not coming in query. However, when I add "Anakans" manually through firestore it shows up.

I am new to flutter and firestore where I am doing the mistake

Solved I set the document field with most value of most recent message. Thus the document no longer a non-existent ancestor. And I can fetch the list of ids. Now my onSendMessage function looks like

  void onSendMessage(String content) {
    if (content.trim() != '') {
      textEditingController.clear();

      var msg = {
        'idFrom': widget.userid,
        'idTo': widget.peerid,
        'timestamp': DateTime.now().millisecondsSinceEpoch.toString(),
        'content': content,
      };

      FirebaseFirestore.instance
          .collection('messages')
          .doc(widget.comid)
          .set(msg);

      var documentReference = FirebaseFirestore.instance
          .collection('messages')
          .doc(widget.comid)
          .collection(widget.comid)
          .doc(DateTime.now().millisecondsSinceEpoch.toString());

      FirebaseFirestore.instance.runTransaction((transaction) async {
        transaction.set(
          documentReference,
          {
            'idFrom': widget.userid,
            'idTo': widget.peerid,
            'timestamp': DateTime.now().millisecondsSinceEpoch.toString(),
            'content': content,
          },
        );
      });
    } else {
      Fluttertoast.showToast(
          msg: 'Nothing to send',
          backgroundColor: Colors.black,
          textColor: Colors.grey[100]);
    }
  }

Upvotes: 0

Views: 618

Answers (1)

Peter Obiechina
Peter Obiechina

Reputation: 2835

I just ran your code. I have seen the issue.

onSendMessage adds a document to this path messages/widget.comid/widget.comid/DateTime.now().millisecondsSinceEpoch.toString()

onSendMessage did not add any document to messages collection, so messages collection is actually empty. Rather, it added a document to this path messages/widget.comid/widget.comid/DateTime.now().millisecondsSinceEpoch.toString(). So messages/widget.comid does not exist because you didn't add any document to messages.

In firestore, a child document can exist even if the parent does not exist.

So '4ePoQ...' as a document, does not exist but it has a child (because you defined the path). Documents that do not exist are in italics (as you can see from your firestore screenshot). When you add ankana manually, you are adding a document to the messages collection.

Solution. Your stream builder should listen to the subcollection widget.comid. This collection has documents (with id DateTime.now().millisecondsSinceEpoch.toString()).

return Scaffold(
  appBar: AppBar(
    title: Text('Title'),
  ),
  body: StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
    stream: FirebaseFirestore.instance
        .collection('messages')
        .doc(widget.comid)
        .collection(widget.comid)
        .snapshots(),
    builder: (BuildContext context,
        AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
      if (snapshot.hasError) return Text('Something went wrong');
      if (snapshot.connectionState == ConnectionState.waiting)
        return CircularProgressIndicator();

      if (snapshot.data.docs.isNotEmpty) {
        print('document exists');
        print(snapshot.data.docs.map((e) => e.data()));
        return Container();
      } else {
        print(snapshot.data.docs);
        print('document does not exist');
        return Container();
      }
    },
  ),
);

Upvotes: 1

Related Questions