0

I have a Custom Object Wishlist which I am serializing and saving/retrieving as JSON from Firebase.

@JsonSerializable()
class Wishlist {
  final String id;
  String name;
  List<Wish>? wishes;

  Wishlist({
    required this.id,
    required this.name,
    this.wishes,
  });

  factory Wishlist.fromJson(Map<String, dynamic> json) =>
      _$WishlistFromJson(json);

  Map<String, dynamic> toJson() => _$WishlistToJson(this);
}

The thing is that I want wishes not to be saved as a documentField but as a subCollection.

How can I achieve that? Right now there is always a field wishes which is null. And then I save wishes in a subcollection. But that does not seem ideal.

How can I fix that? Let me know if you need any more info!

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Chris
  • 1,828
  • 6
  • 40
  • 108

1 Answers1

1

As I understand from your question, your actual schema looks like this:

db
|
--- wishlists (collection)
     |
     --- $wishlistId (document)
           |
           --- id: "wishlistId"
           |
           --- name: "wishlistName"
           |
           --- wishes: [wishObject, wishObject]

If you don't want wishes to be a field within a document, but a sub-collection, then you have to remove the wishes field from within your Wishlist class, and create a schema that looks like this:

db
|
--- wishlists (collection)
     |
     --- $wishlistId (document)
           |
           --- wishes (sub-collection) //
           |    |
           |    --- $docId (document)
           |         |
           |         --- //fields
           |
           --- id: "wishlistId"
           |
           --- name: "wishlistName"

Edit:

According to the latest comments, here is the solution for solving OP's problem.

If the wishes object is needed inside the Wishlist class, and you don't want to have that field present in the document in Firestore, then you can use an annotation, and the field will be totally ignored.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193