You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter调用Firebase:好友请求取消功能仅单侧生效问题排查

好友请求取消功能异常排查与修复

问题现象

开发好友请求系统时出现异常:

  • 发送方的RequestTile包含取消按钮,点击后双方界面的Tile均消失,数据库中请求记录正常删除
  • 接收方的RequestTile包含接受、取消按钮,点击取消按钮时,日志显示cancelFriendRequest方法已执行,但数据库中的请求记录并未被删除

原实现代码

按钮逻辑代码

!isSentRequest
    ? IconButton(
        onPressed: () {
          Utilities.logWarning('Cancelling Requests Received from $requestersId');
          _db.cancelFriendRequest(requestersId);
        },
        icon: Icon(
          Icons.thumb_down_alt,
          color: Colors.red,
          size: 30.r,
        ),
      )
    : Container(),
SizedBox(width: 10.w),
!isSentRequest
    ? IconButton(
        onPressed: () {
          Utilities.logWarning('Accepting Request Received from $requestersId');
          _db.acceptFriendRequest(requestersId);
        },
        icon: Icon(
          Icons.thumb_up_alt,
          color: Colors.green,
          size: 30.r,
        ),
      )
    : IconButton(
        onPressed: () {
          Utilities.logWarning('Cancelling Requests Sent to $requestersId');
          _db.cancelFriendRequest(requestersId);
        },
        icon: Icon(
          Icons.cancel,
          color: Colors.red,
          size: 30.r,
        ),
      ),

原数据库取消请求方法

Future<void> cancelFriendRequest(String _friendUID) async {
  // Remove friend request from users received collection
  await _db
      .collection(USER_COLLECTION)
      .doc(_auth.getCurrentUID())
      .collection(requestsSent)
      .doc(_friendUID)
      .delete();
  Utilities.logWarning('Removed friend request from users received collection');

  // Remove user from requested friends sent collection
  await _db
      .collection(USER_COLLECTION)
      .doc(_friendUID)
      .collection(requestsReceived)
      .doc(_auth.getCurrentUID())
      .delete();
  Utilities.logWarning('Removed user from requested friends sent collection');
}

问题根源

原方法未区分发送方和接收方的集合操作路径:

  • 发送方取消请求时,需要删除自己的requestsSent集合中对方的记录,以及对方的requestsReceived集合中自己的记录
  • 接收方拒绝请求时,需要删除自己的requestsReceived集合中对方的记录,以及对方的requestsSent集合中自己的记录

原方法无论调用者是发送方还是接收方,都优先操作requestsSent集合,导致接收方调用时,尝试删除自己requestsSent中不存在的记录,后续数据库操作也因路径错误无法生效。

修复方案

拆分出针对发送方和接收方的独立方法,明确各自的集合操作路径:

修正后的数据库方法

Future<void> denyReceivedFriendRequest(String _friendUID) async {
  // 移除当前用户接收请求集合中的记录
  _db.collection(USER_COLLECTION).doc(_auth.getCurrentUID()).collection(requestsReceived).doc(_friendUID).delete();
  Utilities.logWarning('Removed friend request from ${_auth.getCurrentUID()} received collection');

  // 移除对方发送请求集合中的记录
  _db.collection(USER_COLLECTION).doc(_friendUID).collection(requestsSent).doc(_auth.getCurrentUID()).delete();
  Utilities.logWarning('Removed friend request from $_friendUID sent collection');
}

Future<void> cancelSentFriendRequest(String _friendUID) async {
  // 移除当前用户发送请求集合中的记录
  _db.collection(USER_COLLECTION).doc(_auth.getCurrentUID()).collection(requestsSent).doc(_friendUID).delete();
  Utilities.logWarning('Removed friend request from ${_auth.getCurrentUID()} sent collection');

  // 移除对方接收请求集合中的记录
  _db.collection(USER_COLLECTION).doc(_friendUID).collection(requestsReceived).doc(_auth.getCurrentUID()).delete();
  Utilities.logWarning('Removed friend request from $_friendUID received collection');
}

按钮逻辑调整

将接收方的取消按钮调用denyReceivedFriendRequest,发送方的取消按钮调用cancelSentFriendRequest即可解决问题。

内容的提问来源于stack exchange,提问作者Technorocker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 11:57:17