It is encouraged in the documentation to use custom types as property types. The documentation states:
If you own/control the desired type, add a fromJson constructor and/or a toJson() function to the type. Note: while you can use json_serializable for these types, you don't have to! The generator code only looks for these methods. It doesn't care how they were created.
But, in fact, the generation does not work in this way. (while using latest versions). The generated toJson does not reference the toJson from my custom type. Therefore, the generated code is not correct and not usable. In the following example, i construct a User class with an address property of my custom type Address. For the generation to be usable, the generated toJson for user would have to reference the Address.toJson, but the generated code does not do so. Instead only user.address is referenced directly. But this is only a reference to the object, no serialization...
Details to this issue:
In order to route, prioritize, and act on this, please include the entire output
of either dart --version or flutter --version, depending on what
you're using.
It is encouraged in the documentation to use custom types as property types. The documentation states:
But, in fact, the generation does not work in this way. (while using latest versions). The generated
toJson
does not reference thetoJson
from my custom type. Therefore, the generated code is not correct and not usable. In the following example, i construct aUser
class with anaddress
property of my custom typeAddress
. For the generation to be usable, the generatedtoJson
for user would have to reference theAddress.toJson
, but the generated code does not do so. Instead onlyuser.address
is referenced directly. But this is only a reference to the object, no serialization...Details to this issue:
package versions
user.dart
address.dart
Expected generated
toJson
forUser
:Actual generated
toJson
forUser
: