Firestore - 'Nested arrays are not supported' (Flutter)

674 views Asked by At

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

There are 1 answers

0
XavierRenegadeAngel On

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