Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Sub-Collection in Document from Flutter app

I am trying to create a sub-collection in a document and set data to documents in the created sub-collection.

I have tried this but the program crashes every time I run this.

  await Firestore.instance
      .collection('/path')
      .document("documentPath")
      .collection('/subCollectionPath')
      .document()
      .setData({
    'TestData': "Data",
  }).then((onValue) {
    print('Created it in sub collection');
  }).catchError((e) {
    print('======Error======== ' + e);
  });

I have also looked online but I cannot find any documentation for it.

Any ideas?

When I try the above code, the app crashes with the following message

   flutter: EVENT StorageTaskEventType.success
   *** First throw call stack:
   (
    0   CoreFoundation                      0x0000000111eb61bb __exceptionPreprocess + 331
    1   libobjc.A.dylib                     0x0000000111454735 objc_exception_throw + 48
    2   Runner                              0x000000010ce707b1 -[FIRFirestore documentWithPath:] + 257
    3   Runner                              0x000000010d10662c getDocumentReference + 124
    4   Runner                              0x000000010d109879 -[FLTCloudFirestorePlugin handleMethodCall:result:] + 2665
    5   Flutter                             0x000000010e5b99a2 __45-[FlutterMethodChannel setMethodCallHandler:]_block_invoke + 115
    6   Flutter                             0x000000010e5d6616 _ZNK5shell21PlatformMessageRouter21HandlePlatformMessageEN3fml6RefPtrIN5blink15PlatformMessageEEE + 166
    7<…>

Lost connection to device.

like image 377
Uma Avatar asked Jan 14 '19 05:01

Uma


People also ask

How do I get sub collection in flutter?

Click on + Add Action. On the right side, search and select the Database action and then select Create Document. Set the Collection to the subcollection i.e messages. You can create a subcollection document under an existing reference if there is a subcollection defined.

What is FlutterFire?

FlutterFire is a set of Flutter plugins which connect your Flutter application to Firebase.

How do I delete a Subcollection in firestore flutter?

To delete an entire collection or subcollection in Cloud Firestore, retrieve all the documents within the collection or subcollection and delete them. If you have larger collections, you may want to delete the documents in smaller batches to avoid out-of-memory errors.


1 Answers

 final databaseReference = Firestore.instance;
databaseReference.collection('main collection name').document( unique id).collection('string name').document().setData(); // your answer missing **.document()**  before setData

this is the right Syntex

like image 103
Gnziet Avatar answered Oct 07 '22 23:10

Gnziet