fizahpanda103
fizahpanda103

Reputation: 69

a document path must be a non-empty string 'package:cloud_firestore/src/collection_reference.dart'

I'm trying to develop chat application. But I am getting error if I want to send a message to a chat. And I dont know where should I fix my error. Can someone help me and tell me what I did wrong and correct me somewhere?

P/s: im still new for chat development in flutter

This is the error i got when i try to send message

void initState() {
    super.initState();
    listScrollController.addListener(_scrollListener);

    groupChatId = '';
    readLocal();
}

readLocal() async {
    prefs = await SharedPreferences.getInstance();
    id = prefs.getString('id');
    if (id != null && !id.isEmpty) {
      if (id.hashCode <= peerId.hashCode) {
        groupChatId = '$id-$peerId';
      } else {
        groupChatId = '$peerId-$id';
      }
      FirebaseFirestore.instance
          .collection('User')
          .doc(id)
          .update({'chattingWith': peerId});

      setState(() {});
    }
    
    void onSendMessage(String content, int type) {
    if (content.trim() != '') {
      textEditingController.clear();

      var docRef = FirebaseFirestore.instance
          .collection('Message')
          .doc(groupChatId)
          .collection(groupChatId)
          .doc(DateTime.now().millisecondsSinceEpoch.toString());

      FirebaseFirestore.instance.runTransaction((transaction) async {
        transaction.set(docRef, {
          'idFrom': id,
          'idTo': peerId,
          'timestamp': DateTime.now().millisecondsSinceEpoch.toString(),
          'content': content,
          'type': type
        });
      });
      listScrollController.animateTo(0.0,
          duration: Duration(milliseconds: 300), curve: Curves.easeOut);
    } else {
      Fluttertoast.showToast(
          msg: 'Nothing to send',
          backgroundColor: Colors.black,
          textColor: Colors.white);
    }
}

Upvotes: 0

Views: 3025

Answers (1)

Denis-Gabriel Craciun
Denis-Gabriel Craciun

Reputation: 21

That is because you are passing an empty value to the database. I had a similar problem where I was passing the last message to the database, and I was initializing it with "". Try putting something like a space " " instead.

Upvotes: 2

Related Questions