【问题标题】:a document path must be a non-empty string 'package:cloud_firestore/src/collection_reference.dart'文档路径必须是非空字符串“package:cloud_firestore/src/collection_reference.dart”
【发布时间】:2021-08-18 08:55:09
【问题描述】:

我正在尝试开发聊天应用程序。但是,如果我想向聊天发送消息,我会收到错误消息。而且我不知道我应该在哪里修复我的错误。有人可以帮助我并告诉我我做错了什么并在某个地方纠正我吗?

P/s:我还是 Flutter 中的聊天开发新手

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);
    }
}

【问题讨论】:

  • 兄弟,我认为您的群聊 ID 为空,因此请确保它不为空
  • 如何让它不为空?对不起,我不知道如何解决它
  • 正如@Muhammad Arbaz Zafar 所说,您的groupChatId 似乎是问题所在。您可以在拨打FirebaseFirestore.instance.collection().doc()时检查它的值吗?
  • 我试着照你说的做,消息被保存到数据库中,但 idfrom 和 idto 为空
  • 你在哪里定义peerId?

标签: flutter mobile google-cloud-firestore


【解决方案1】:

那是因为您将一个空值传递给数据库。我有一个类似的问题,我将最后一条消息传递给数据库,我用“”初始化它。试着用空格“”代替。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-23
    • 2017-04-23
    • 1970-01-01
    • 2016-10-13
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    相关资源
    最近更新 更多