简体   繁体   中英

Cloud firestore Flutter cannot set data on a document

I am trying to make a flutter app where I collect some student data and store it on to firestore. I want to add documents regarding users in a collection named users and inside documents which have a custom id, say uid.

The problem I am facing is that I cannot set data on such a document, where neither that particular document, nor the collection exists. Now, this is not actually the cause of the problem since it works when using the JavaScript library for cloud firestore.

My dependencies:

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

Code so far:

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;
  }
}

My firestore rules:

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

Error that I am facing

[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.

Please note that I can read data from the firestore. There is no error there. So the security rules are also not an issue. Also, the collection users and hence no document inside of it exists when running the function.

I tried it out in some other android (java) project and it works.

I need the ability to have custom IDs for the documents for efficient retrieval later.

The code seems to be fine (it's a bit dated though), but the issue might've been caused by using an outdated version of cloud_firestore plugin.

Currently, the code should look similarly to

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;
  }
}

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