Flutter cloud_firestore插件Android上的交易

时间:2018-05-10 19:31:58

标签: firebase dart google-cloud-firestore flutter

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

  

E / AndroidRuntime(26208):致命异常:AsyncTask#2   E / AndroidRuntime(26208):处理:io.flutter.plugins.googlesigninexample,PID:26208   E / AndroidRuntime(26208):java.lang.RuntimeException:>执行doInBackground()时发生错误   E / AndroidRuntime(26208):在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):在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):引起:java.lang.IllegalArgumentException:>提供的文档引用来自不同的Firestore实例。   E / AndroidRuntime(26208):在com.google.firebase.firestore.FirebaseFirestore.zza(未知来源:17)   E / AndroidRuntime(26208):在com.google.firebase.firestore.Transaction.get(未知来源: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):在android.os.AsyncTask $ 2.call(AsyncTask.java:333)   E / AndroidRuntime(26208):at java.util.concurrent.FutureTask.run(FutureTask.java:266)   E / AndroidRuntime(26208):...还有4个   D / FlutterNativeView(26208):handlePlatformMessage回复分离视图,channel = plugins.flutter.io / cloud_firestore   I / FirebaseAuth(26208):[FirebaseAuth:]通过FirebaseOptions加载模块。   I / FirebaseAuth(26208):[FirebaseAuth:]准备创建与gms实现的服务连接

以下是基于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"
扑克医生:

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!

1 个答案:

答案 0 :(得分:1)

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

我在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 ...完全与主要版本无关。

我从一个错误

跟踪了我的问题
  

原因:java.lang.IllegalArgumentException:提供的文档参考来自其他Firestore实例。

所以我删除了行

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

最终使交易按预期运行。我认为这种想法是不要同时发生多个实例。