2

really struggling getting my data structure into firestore. the structure is something like this:

 class1List[class1_index].class2List[class2_index].class3List[class3_index] etc...

ok so im using json_serializable and its tojson and fromjson methods generated by build_runner. each new class1 object added to the class1List gets a new doucment made for it in firestore, inside of that document, im trying to store all of the nested lists of objects.

so far, class1 documents are being created, then by using the document.id im able to add class2 objects to class2List inside of class1 via class1 method:

    void createClass2_Object(String name, class1_index) async {
class2List.add(Class2(name: name));
        await FirebaseFirestore.instance
            .collection('users')
            .doc(AuthService().currentUser?.uid)
            .collection('class1')
            .doc(docID)
            .set(class1List[class1_index].toJson());
        
      }

the above works perfectly well, the problem arises when I try to repeat this process for the class3List of class3 objects inside of class2.

at runtime, when i try to call the method of class2 to createClass3_Object() and .set() the class1 document in firestore:

           void createClass3_Object({required name, required duration, class1_index}) async {

class3List.add(Class3(name: name, duration: duration));

await FirebaseFirestore.instance //error is here
    .collection('users')
    .doc(AuthService().currentUser?.uid)
    .collection('class1')
    .doc(class1List[class1_index].docID)
    .set(class1List[class1_index].toJson());

i get the error 'Nested arrays are not supported'

not really sure how to work around this, i read somewhere that firestore is now supposed to support the nested arrays, not sure if thats still the case.

one thing i should mention is i tried creating a new collection and document for class2 objects, but oddly enough I still go the same error 'Nested arrays are not supported'

Question Updated:

Here are the models' toJson methods, as generated by build_runner and json_serailizable:

// located in class1.g.dart

 Map<String, dynamic> _$Class1ToJson(Class1 instance) => <String, dynamic>{
          'name': instance.name,
          'class2List': instance.class2List.map((e) => e.toJson()).toList(),
          'docID': instance.docID,
        };

//located in class2.g.dart
    
    Map<String, dynamic> _$class2ToJson(class2 instance) => <String, dynamic>{
          'name': instance.name,
          'class3List': instance.class3List.map((e) => e.toJson()).toList(),
        };

This continues down the chain of data models in the nested structure of

class1List[class1_index].class2List[class2_index].class3List[class3_index] etc... 
  • 2
    By nested array, it means to say your `class1List[class1_index].toJson()` that you're trying to set in the document under collection `class1` is a nested array (means array inside array) and there's no type on Firestore to store Nested Array, You can go only further to an Array of Map/Objects. So, you've to figure out a way to remove the nested array and make it an array which contains objects in which there's a key which points to another doc where the inner array is saved. – Lalit Fauzdar Apr 14 '22 at 05:20
  • thanks for your comment. can you elaborate a little on how my dart data structure should look, right now lists of objects containing lists of objects etc for 6 levels and since that doesn't work in Firestone, what's a good alternative structure that functionally does the same thing of like a chain of references? – XavierRenegadeAngel Apr 14 '22 at 05:57
  • @LalitFauzdar can other databases handle this nested structure? is this a limitation unique to firestore or is this typical of databases in general? – XavierRenegadeAngel Apr 14 '22 at 06:05
  • @LalitFauzdar Why not add that information as an asnwer? :) – Alex Mamo Apr 14 '22 at 07:03
  • 1
    @XavierRenegadeAngel AFAIK, This limitation is unique to Firestore as they intend to provide nested collections instead of handling Nested arrays. So, you've to extract the inner arrays as separate docs in any other parent/child collection. – Lalit Fauzdar Apr 14 '22 at 12:53
  • @AlexMamo I always prefer to provide such kind of hints or suggestions in comments, instead of writing them as an answer. I won't say that's an answer, a clarification instead, answer would be what helps the OP. – Lalit Fauzdar Apr 14 '22 at 12:55
  • @lalitfauzdar i tried creating a new collection and document for class2 objects, but oddly enough I still go the same error 'Nested arrays are not supported' – XavierRenegadeAngel Apr 14 '22 at 14:09
  • 1
    You're getting this error, not because you've to create new doc or collection but because the data you're trying to set is causing the issue. You've to segregate the data which is `class1List[class1_index].toJson()`. Post it here. – Lalit Fauzdar Apr 14 '22 at 14:30
  • @LalitFauzdar not sure what you mean by this, can you say it another way? or point me in the right direction – XavierRenegadeAngel Apr 14 '22 at 14:33
  • The issue is in the JSON data and not in Firebase. If you can provide me the data, I can help. – Lalit Fauzdar Apr 14 '22 at 14:34
  • to clarify, i tried creating subcollections to handle the nested arrays instead of trying to store these nested arrays all in one document , and still got the exact same error – XavierRenegadeAngel Apr 14 '22 at 14:35
  • @LalitFauzdar ok i updated the question and added the toJson methods i'm trying to use. I hope this helps, let me know if you need more information – XavierRenegadeAngel Apr 15 '22 at 21:23
  • Firebase doesn't support this type of structure: `class1List[class1_index].class2List[class2_index].class3List[class3_index]` where an array's child is also an array. – Lalit Fauzdar Apr 15 '22 at 21:44
  • @LalitFauzdar so should i use nested maps instead? new to programming not really sure what a good alternative data structure would be in this case. or should i instead keep my dart data structure of nested arrays, and just create subcollections and docs in firestore? – XavierRenegadeAngel Apr 15 '22 at 21:47
  • @LalitFauzdar i am now trying to use a nested maps approach instead and am still having some issues, I'm made a new post for this here https://stackoverflow.com/questions/71897225/firestore-data-modellling-issues – XavierRenegadeAngel Apr 16 '22 at 20:25

1 Answers1

0

the recursive 'arrays of object' approach I was taking with my data models simply is not supported by Firestore. I still haven't found a good alternative but I know it will probably have to involve maps of objects, arrays of maps, or some other scheme using maps. I have been trying alternative structures and am still having issues.

You can see what I'm trying in my new post Firestore Data Modellling Issues