首页 > 解决方案 > Android 未捕获 Firestore 事务 PlatformException

问题描述

我在 Flutter 和 Firebase 中构建随机的 1:1 聊天应用程序。我transaction用于将用户添加到聊天中,因为必须避免将超过 1 个用户的竞争条件添加到聊天中。聊天必须是 1:1。

如果超时,我需要捕获transaction错误。transaction

错误仅在 iOS(而非 Android)上捕获。仅在 iOS 上它会捕获错误,如下所示:

PlatformException(9,事务重试失败。:事务中读取的每个文档也必须写入该事务中。,null)

在调试模式下的 Android 上,应用程序冻结并抛出错误message_codecs.dart

throw PlatformException(代码:errorCode,消息:errorMessage,详细信息:errorDetails);

这是交易:

      Future<bool> addUser() async {

    try {
      await Firestore.instance.runTransaction((transaction) async {
        DocumentSnapshot chatRoomDocSnapshot =
            await transaction.get(chatRoomDocRef);
        bool userInChat = await chatRoomDocSnapshot[‘userInChat'];

        if (userInChat == false) {
            await transaction.update(chatRoomDocSnapshot.reference, {
              ‘userInChat': true,
            });
        }
      });

    await chatUserRef.setData({
      ‘User’: user,
    });

      userAdded = true;


    } catch (e) {
print(e);
      userAdded = false;
    }
    return userAdded;
      }

我正在使用插件cloud_firestore: ^0.12.5+1

有人看到这个插件问题吗?

为什么只在 Android 上出现此错误?

感谢帮助!

标签: firebaseflutterdartgoogle-cloud-firestore

解决方案


推荐阅读