简体   繁体   中英

Flutter cloud_firestore plugin Transactions on Android

Has anyone successfully run a transaction using the cloud_firestore plugin? I'm getting the following error:

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): Cause d 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

Here is the code based on 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:

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!

I had the same problem for some time where my app will fatally crash whenever I try to run a transaction.

I initiated my app with in main.dart as such:

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

after authentication, I direct to homescreen.dart, which was using new Firestore.instance... completely unrelated to the one in main.

I traced my problem from an error that says

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

So I removed the lines

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

and finally got the transaction to run as expected. I think the idea is to not have multiple instances happening at the same time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM