Search code examples
flutterdartgoogle-cloud-firestorecollectionsdocument

Create Collection and Documents Dynamically (Firestore, Flutter)


I am trying to make a system where a person can submit data to a collection. The Collection name and document name are based on the variable put into it and sometime will not have been created yet. I want to make it so that if the collection or document don't exist then they will automatically be made.

I have had it work before but for some reason this time it is not working.

The current code which is not working is:

await FirebaseFirestore.instance
        .collection('user-content')
        .doc('councils')
        .collection('council-paydates')
        .doc(council)
        .collection(year)
        .add({"data": "data"});

The variables of the above are council and year.

The code that worked in the past for similar but different purpose is as follows

await FirebaseFirestore.instance
            .collection('reviews')
            .doc('councils')
            .collection('reviews')
            .doc(widget.council)
            .collection('councilRatingReviews')
            .add({
          "POST_DETAILS": {
            "UID": userId,
            "USERNAME": displayName ?? '',
            "DATE_OF_POST": _dateOfPost,
            "ANONYMOUS": anonymousComment,
          },
          "COUNCIL_REVIEW": {
            "TITLE": _titleTextController.text.trim(),
            "REVIEW": _commentTextController.text.trim(),
            "COUNCIL": widget.council,
            "OVERALL_RATING": _rating,
          },});

The variables for dynamically creating the collection and documents for above were widget.council.

Could anyone help me into how to make it so that the collection and document paths are made if they don't currently exist.

EDIT: The year String that was being input had a '/' character that was not allowed. I just converted the '/' to a '-' and it started working.

SOLUTION

The variable "year" i was trying to place into the firebase path had a "/" character in it which is not allowed the following is how i made it work.

String year = "2024/2025"
String convertedYear = year.replaceAll('/', '-');//2024-2025

await FirebaseFirestore.instance
        .collection('user-content')
        .doc('councils')
        .collection('council-paydates')
        .doc(council)
        .collection(convertedYear)//changed from year to convertedYear
        .add({"data": "data"});


Solution

  • Posting the answer as community wiki, so that it will be easier to find solution for others who are facing similar issues.

    @Ankere feel free to add if I missed anything

    The Document's name should not have an escaping character like /. Which may lead to these kinds of issues. Hence, while using date as document name replace slash(/) with hyphen(-).

    String year = "2024/2025"
    String convertedYear = year.replaceAll('/', '-');//2024-2025
    
    await FirebaseFirestore.instance
            .collection('user-content')
            .doc('councils')
            .collection('council-paydates')
            .doc(council)
            .collection(convertedYear)//changed from year to convertedYear
            .add({"data": "data"});
    

    Follow these best practices in naming conventions of firestore.