【问题标题】:Cloud firestore Flutter cannot set data on a documentCloud firestore Flutter 无法在文档上设置数据
【发布时间】:2019-07-22 04:42:05
【问题描述】:

我正在尝试制作一个颤振应用程序,在其中收集一些学生数据并将其存储到 Firestore。我想在名为 users 的集合中添加有关用户的文档,并在具有自定义 id 的文档内部添加文档,比如 uid。

我面临的问题是我无法在这样的文档上设置数据,其中既不存在该特定文档,也不存在该集合。现在,这实际上不是问题的原因,因为它在将 JavaScript 库用于 Cloud Firestore 时有效。

我的依赖:

dependencies:
  flutter:
    sdk: flutter
  google_sign_in: ^4.0.1+1
  scoped_model: ^1.0.1
  http: ^0.12.0+1
  shared_preferences: ^0.4.2
  rxdart: ^0.20.0
  file_picker: ^1.2.0
  flutter_pdf_viewer: ^0.2.0
  firebase_auth: ^0.8.1+4
  firebase_core: ^0.3.1+1
  firebase_messaging: ^4.0.0+1
  firebase_storage: ^2.1.0+1
  cloud_firestore: ^0.9.5+2

到目前为止的代码:

Future<bool> storeStudentData(Map<String, Object> data) async {
  String uid = authenticatedUser.id;

  DocumentReference docRef = db.collection('users').document(uid);

  try {
    await docRef.setData(data);
    print('Done setting data');
    return true;
  } catch (err) {
    print('EERRRR setting data');
    return false;
  }
}

我的 Firestore 规则:

service cloud.firestore {
  match /databases/{database}/documents {
  match /{document=**} {
    allow read, write: if request.auth != null;
    }
  }
}

我面临的错误

[ERROR:flutter/shell/platform/android/platform_view_android_jni.cc(40)] 
java.lang.NoSuchMethodError: No virtual method 
set(Ljava/util/Map;)Lcom/google/android/gms/tasks/Task; in class 
Lcom/google/firebase/firestore/DocumentReference; or its super classes 
(declaration of 'com.google.firebase.firestore.DocumentReference' appears in 
/data/app/com.example.rla_official-1/base.apk:classes2.dex)
E/flutter (24452):      at 
io.flutter.plugins.firebase.cloudfirestore.
CloudFirestorePlugin.onMethodCall(CloudFirestorePlugin.java:523)
E/flutter (24452):      at 
io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler
.onMessage(MethodChannel.java:201)
E/flutter (24452):      at 
io.flutter.view.FlutterNativeView$PlatformMessageHandlerImpl
.handleMessageFromDart(FlutterNativeView.java:188)
E/flutter (24452):      at io.flutter.embedding.engine.FlutterJNI.
handlePlatformMessage(FlutterJNI.java:202)
E/flutter (24452):      at android.os.MessageQueue.nativePollOnce(Native 
Method)
E/flutter (24452):      at 
android.os.MessageQueue.next(MessageQueue.java:328)
E/flutter (24452):      at android.os.Looper.loop(Looper.java:148)
E/flutter (24452):      at 
android.app.ActivityThread.main(ActivityThread.java:6339)
E/flutter (24452):      at java.lang.reflect.Method.invoke(Native Method)
E/flutter (24452):      at 
com.android.internal.os.
ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1084)
E/flutter (24452):      at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:945)
E/flutter (24452):
F/flutter (24452): 
[FATAL:flutter/shell/platform/android/platform_view_android_jni.cc(77)] 
Check failed: CheckException(env).
F/libc    (24452): Fatal signal 6 (SIGABRT), code -6 in tid 24452 
(le.rla_official)
Lost connection to device.

请注意,我可以从 Firestore 中读取数据。那里没有错误。所以安全规则也不是问题。 此外,在运行该函数时,集合用户和其中不存在文档。

我在其他一些 android (java) 项目中尝试过它,它可以工作。

我需要为文档设置自定义 ID,以便以后高效检索。

【问题讨论】:

  • 您确定错误来自您共享的代码吗?错误消息是关于DocumentReference.set()(确实不存在),但您的代码正在调用setData(堆栈跟踪没有在任何地方提及)。
  • 有一个关于同样问题的问题,这里是github.com/flutter/flutter/issues/28082,它说版本不兼容。我一直在混合和匹配这么多版本号,但没有组合工作
  • 我确实找到了解决方法。我设置了云功能以将数据添加到 firestore db,但如果库代码正常工作会很有帮助

标签: android flutter google-cloud-firestore


【解决方案1】:

代码看起来不错(虽然有点过时),但问题可能是由于使用了过时版本的 cloud_firestore 插件造成的。

目前,代码应该类似于

Future<bool> storeStudentData(Map<String, dynamic> data) async {
  String uid = authenticatedUser.id;

  DocumentReference docRef = FirebaseFirestore.instance.collection('users').document(uid);

  try {
    await docRef.set(data);
    debugPrint('Done setting data');
    return true;
  } catch (err) {
    debugPrint('EERRRR setting data');
    return false;
  }
}

【讨论】:

    猜你喜欢
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 2022-06-24
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    相关资源
    最近更新 更多