Flutter cloud_firestore插件Android上的交易

问题描述 投票:3回答:1

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

E / AndroidRuntime(26208):FATAL EXCEPTION: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):原因d by:java.lang.IllegalArgumentException:>提供的文档引用来自不同的Firestore实例。 E / AndroidRuntime(26208):在com.google.firebase.firestore.FirebaseFirestore.zza(未知来源:17)E / AndroidRuntime(26208):at com.google.firebase.firestore.Transaction.get(Unknown来源:2) E / AndroidRuntime(26208):在io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin $ 4.doInBackground(CloudFirestorePlugin.java:321)E / AndroidRuntime(26208):在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!
firebase dart google-cloud-firestore flutter
1个回答
3
投票

一段时间我遇到同样的问题,每当我尝试运行一个事务时,我的应用程序就会崩溃。

我在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中的那个完全无关。

我从一个错误中追踪我的问题

引起:java.lang.IllegalArgumentException:提供的文档引用来自不同的Firestore实例。

所以我删除了这些行

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

最后让交易按预期运行。我认为这个想法是不会同时发生多个实例。

© www.soinside.com 2019 - 2024. All rights reserved.