Skip to content

Attributes which aren't in the constructor aren't serialized #274

Closed
@joostlek

Description

@joostlek

Hey,

I found an issue whilst working on my flutter app.

(Bit of context first)
I have an Activity, it's a base model, containing a DateTime, Firestore DocumentID, user and a type. That type is an enum, and currently I have ~11 activities, all inheriting the Activity class. And adding more attributes. This all works.

To deserialize the activities, I check on the Type attribute, and get the right class to construct the Activity with.

But because the Type of each different class is the same, I didn't include it as an argument in the constructor (When I am constructing a CreateUser, I don't want to send the type again because if I create a CreateUser, I want a CreateUser, not a CreateUser with a RemoveUser type)

So all the extended models have a constructor with a super constructor, and that type is not passed as argument but is just in the super call (CreateUser(.....) : Super(Activities.CreateUser).

This would create an object with type: CreateUser.

But when I am Serializing, it doesnt Serialize the type attribute. So when it's stored in Firestore it doesn't show what Type it is, thus I can't deserialize it, because I don't know what constructor I need to use.

I fixed this by just putting a , [Activities type] to the constructor.

Relevant code is in GitHub.com/joostlek/eros
(The lib/models/activity/Activity is the main class)

Thanks
Keep up the great work you re doing

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions