Firebase get call在Android上有效,但在iOS上有奇怪的行为

时间:2019-04-22 07:17:55

标签: ios firebase dart flutter

我有一个Flutter应用程序,已经在Android上使用了将近2个月的客户端,现在我还必须制作iOS版本。

当我尝试使用userAccount.id运行get调用时,我从firebase返回了6次数据,但是随后出现“交易失败所有重试”错误。

我尝试了所有可以在Google上找到的内容。

注意:Firebase中的流数据正常工作

我正在使用cloud_firestore flutter软件包

功能如下:

  static Future<WebRequestResponse> getUserAccountByID(String id) async {
    if (!await isInternetConnectionAvailable()) {
      return WebRequestResponse(false, null, null,
          errorType: ErrorType.ERROR_NO_INTERNET_CONNECTION);
    }
    DocumentSnapshot ds;
    final TransactionHandler getTransaction = (Transaction tx) async {
      ds = await tx.get(userAccountsCollection.document(id));
      print(ds.data);
      return ds.data;
    };

    WebRequestResponse response = WebRequestResponse(false, null, null);
    return Firestore.instance.runTransaction(getTransaction).then((jsonData) {
      print(jsonData);
      UserAccount userAccount = UserAccount.fromJson(jsonData);
      print(userAccount.id);
      if (userAccount.id != null) {
        response.success = true;
        response.data = userAccount;
        return response;
      } else {
        response.success = false;
        response.errorType = ErrorType.ERROR_USER_NOT_FOUND;
        return response;
      }
    }).catchError((error) {
      print('get_user_account_by_id_error: $error');
      response.success = false;
      response.error = error;
      response.errorType = ErrorType.ERROR_UNKNOWN;
      return response;
    });
  }

这是我得到的答复:

flutter: {id: Edaavf39l4Y3cbfGy4RblYSdy1p1, profilePictureURL: https://firebasestorage.googleapis.com/v0/b/fr<REDACTED REST OF JSON>}
flutter: {id: Edaavf39l4Y3cbfGy4RblYSdy1p1, profilePictureURL: https://firebasestorage.googleapis.com/v0/b/fr<REDACTED REST OF JSON>}
flutter: {id: Edaavf39l4Y3cbfGy4RblYSdy1p1, profilePictureURL: https://firebasestorage.googleapis.com/v0/b/fr<REDACTED REST OF JSON>}
flutter: {id: Edaavf39l4Y3cbfGy4RblYSdy1p1, profilePictureURL: https://firebasestorage.googleapis.com/v0/b/fr<REDACTED REST OF JSON>}
flutter: {id: Edaavf39l4Y3cbfGy4RblYSdy1p1, profilePictureURL: https://firebasestorage.googleapis.com/v0/b/fr<REDACTED REST OF JSON>}
flutter: get_user_account_by_id_error: PlatformException(9, Transaction failed all retries., null)

如您所见,数据已通过事务内部的打印正确打印出,但是发生了6次,之后又失败了。知道为什么会这样以及如何解决吗?

非常感谢您

1 个答案:

答案 0 :(得分:0)

因此,经过数周的努力并尝试了一些随机的事情之后,我发现这可能是因为我在虚拟机上运行不流畅(尚无法确认)。

无论如何,对我来说,解决方案是在每个tx.get调用之后,我必须添加一个具有相同文档ID的tx.set调用,并使用空数据进行更新。我不知道为什么,或如何运作,但确实如此。我可以回答,以防将来有人遇到相同的问题。