Closed Miamoto-Musashi closed 2 years ago
Hi @Miamoto-Musashi
I think is bad practice to generate objects as null
. It will show lint errors without Object
class and it not possible generate class without the properties. And you will be forced to implement everything manually into County
class. It is equal to re-implementation of dynamic value.
// USER
{
"__className": "User",
"key": "110afe06-ec31-4d9b-b933-d9e060c7bcb7",
"country": null, <---// it will show lint error without `dynamic`
},
How can I then tell the generator that I want the country
property to be of type Country
class?
at them moment the only workaround I found is to put null in the jsonc file so that the generator create the property with a dynamic type and I can compile.
thanks
How can I then tell the generator that I want the
country
property to be of typeCountry
class?
You can tell to the generator by adding empty brackets {}
then the generator will generate an empty class without parameters instead of dynamic. But you will need to correct it a little bit to avoid lint errors.
{
"__className": "User",
"id": "110",
"country": {} // <- to tell generate class "Country" instead of dynamic.
}
@Miamoto-Musashi I will look at this how-to it can be improved without lint errors.
@Miamoto-Musashi HI.
It looks like a bug. I fill fix it with this solution.
class Country {
Country();
factory Country.fromJson(Map<String, dynamic> json) {
// TODO: implement fromJson
throw UnimplementedError('Country.fromJson($json) is not implemented');
}
Map<String, dynamic> toJson() {
// TODO: implement toJson
throw UnimplementedError();
}
}
Hi @iamarnas In real life I would probably prefer to have another Country object declared in jsonc file and refer to that one. having an empty Country object can be misleading.
there is another UseCase where reference would be preferred: think about this jsonc file snippet:
{
"distance":12.1,
"positions":null,
"bike":null
}
here I have a single object (Bike
) referenced and a List of objects (Position
); now I'm using null to have dynamic generated so that I can address both cases, in case of strict typing I would prefer something like
{
"distance":12.1,
"positions":[{@Position}],
"bike":{@Bike}
}
@Miamoto-Musashi It not easier to be better generate an empty object and changes the import from the right mouse button context to navigate to the implemented class.
{
"positions":[{}],
"country": {} // <- you can rename your object how you want be changing key name.
}
Or I do not understand what really you mean!?
yes it is easier as a workaround but I see this tool to help with automation, boilerplate and portability if I have to adjust every generated class by hand its loosing some of the util
what I think would be useful is some kind of reference from one object in the jsonc file to another object in the same jsonc file
You using this generator as tool to speed up coding? Such changes will throw the error by overriding Dart classes from JSON
not sure to understand; say we have a User object and a Country object and a reference from Country to User like a 1:N relation in RDBMS; we can have the Country generated with a ref to User object,
in json notation would become:
off course if there is already a Country object it's going to be overridden or you can check if already exists and throw a warning but link the 2 objects.
You using this generator as tool to speed up coding? Such changes will throw the error by overriding Dart classes from
JSON
I think this generator have great potential in the fields of:
Hi,
Any progress on how to parse refs?
I think it's a better solution to use '@ClassName' because sometimes the field name it's not the same as the class name.
Example:
{photo: @File}
Thanks for the amazing plugin.
@arcas0803 Hi :wave: Maybe you looking for that. Force change type by splitting with the dot.
{
photo.file: { // generates: File photo; instead Photo photo;
/*... */
}
}
You are right. It would be amazing to have some documentation on the batch Converter (With the .jsonc). Again, amazing work.
You are right. It would be amazing to have some documentation on the batch Converter (With the .jsonc). Again, amazing work.
@arcas0803 It is in beta right now when it will be stable. I will update the documentation. You can play with it and test. Any suggestion is welcome. Have fun with coding and I'm so glad you like it 😊
Closes it because the generator already supports it and no more reports from users.
would be nice to have the possibility to put object type in the json
only way to have the country not String type is put it as
null
and use thedynamic
type on conversion. Would be nice to have the feature to put the Country object instead...using jsonc generation probably we can use some kind of placeholder //@Country@ ?
just a quick idea