firebase - Flutter cloud_firestore 插件 Android 上的事务

有没有人使用 cloud_firestore 插件成功运行过交易?我收到以下错误:

E/AndroidRuntime(26208): FATAL EXCEPTION: AsyncTask #2 E/AndroidRuntime(26208): Process: io.flutter.plugins.googlesigninexample, PID: 26208 E/AndroidRuntime(26208): java.lang.RuntimeException: An error occurred while >executing doInBackground() E/AndroidRuntime(26208): at android.os.AsyncTask$3.done(AsyncTask.java:353) E/AndroidRuntime(26208): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383) E/AndroidRuntime(26208): at java.util.concurrent.FutureTask.setException(FutureTask.java:252) E/AndroidRuntime(26208): at java.util.concurrent.FutureTask.run(FutureTask.java:271) E/AndroidRuntime(26208): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) E/AndroidRuntime(26208): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) E/AndroidRuntime(26208): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) E/AndroidRuntime(26208): at java.lang.Thread.run(Thread.java:764) E/AndroidRuntime(26208): Caused by: java.lang.IllegalArgumentException: >Provided document reference is from a different Firestore instance. E/AndroidRuntime(26208): at com.google.firebase.firestore.FirebaseFirestore.zza(Unknown Source:17) E/AndroidRuntime(26208): at com.google.firebase.firestore.Transaction.get(Unknown Source:2) E/AndroidRuntime(26208): at io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin$4.doInBackground(CloudFirestorePlugin.java:321) E/AndroidRuntime(26208): at io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin$4.doInBackground(CloudFirestorePlugin.java:316) E/AndroidRuntime(26208): at android.os.AsyncTask$2.call(AsyncTask.java:333) E/AndroidRuntime(26208): at java.util.concurrent.FutureTask.run(FutureTask.java:266) E/AndroidRuntime(26208): ... 4 more D/FlutterNativeView(26208): handlePlatformMessage replying to a detached view, channel=plugins.flutter.io/cloud_firestore I/FirebaseAuth(26208): [FirebaseAuth:] Loading module via FirebaseOptions. I/FirebaseAuth(26208): [FirebaseAuth:] Preparing to create service connection to gms implementation

这是基于 https://github.com/flutter/plugins/tree/master/packages/cloud_firestore#usage 的代码:

   final DocumentReference postRef =
    Firestore.instance.document('posts/post1');
    Firestore.instance.runTransaction((Transaction tx) async {
      DocumentSnapshot postSnapshot = await tx.get(postRef);
      if (postSnapshot.exists) {
      await tx.update(postRef,
          <String, dynamic>{'likesCt': postSnapshot.data['likesCt'] + 1});
      }
    });

pubspec.lock:

  cloud_firestore:
    dependency: "direct main"
    description:
      name: cloud_firestore
      url: "https://pub.dartlang.org"
    source: hosted
    version: "0.7.0+2"

flutter 医生:

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, v0.3.1, on Microsoft Windows [Version 
10.0.16299.431], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK 27.0.3)
[√] Android Studio (version 3.1)
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
[√] IntelliJ IDEA Community Edition (version 2018.1)
[√] Connected devices (1 available)

• No issues found!

最佳答案

一段时间以来,我遇到了同样的问题,每当我尝试运行事务时,我的应用程序都会致命地崩溃。

我在 main.dart 中启动了我的应用程序:

final FirebaseApp app =
      await FirebaseApp.configure(options: _options(), name: 'testauth');
final Firestore firestore = Firestore(app: app);
  await firestore.settings(timestampsInSnapshotsEnabled: true);

身份验证后,我直接访问 homescreen.dart,它正在使用新的 Firestore.instance... 与 main 中的完全无关。

我从一个错误中找到了我的问题

Caused by: java.lang.IllegalArgumentException: Provided document reference is from a different Firestore instance.

所以我删除了这些行

final Firestore firestore = Firestore(app: app);
await firestore.settings(timestampsInSnapshotsEnabled: true);

终于让事务按预期运行。我认为这个想法是不要同时发生多个实例。

https://stackoverflow.com/questions/50280256/

相关文章:

cookies - Dart/Flutter 身份验证实现

dart - Flutter:在第一次加载时将数据预加载到 Firestore 本地缓存中

visual-studio-code - Flutter:具有@override 列表的 VSCod

Flutter - 加载 Assets 进行测试

android - 如何在 flutter 和平台之间传输麦克风数据?

dart - 如何获取当前小部件的偏移量

dart - flutter 热重载

android-studio - Android Studio 在创建新的 Flutter 项目时卡

flutter - 如何在单元测试期间访问 StatefulWidget 的状态内容?

dart - 如何运行 `source_gen` ?