Json-to-Dart-Model icon indicating copy to clipboard operation
Json-to-Dart-Model copied to clipboard

Change non-cast variable

Open tranhuongk opened this issue 1 year ago • 9 comments

Change: json as int -> int.tryParse(json.toString()) json as double -> double.tryParse(json.toString()) json as String? -> json?.String() json as List? -> List.from(json) json as Map? -> Map.from(json)

tranhuongk avatar Aug 02 '22 17:08 tranhuongk

@hiranthaR

tranhuongk avatar Aug 05 '22 09:08 tranhuongk

@hiranthaR I just released an add-on for our team to use. if you don't intend to merge my pull request then let me know so i can officially release it as a copy. https://marketplace.visualstudio.com/items?itemName=HuongTran.json-to-dart-enhanced

tranhuongk avatar Aug 08 '22 02:08 tranhuongk

@tranhuongk Hi 👋 It's great to see people who want to join and help create a good product together. I'm on vacation right now and don't have a chance to check the code. I want to explain that this generator follow code guide style by Dart team and Dart style. I don't know reason why you want change as default with non cast type. But would be nice to keep default style as Dart team. Maybe it would be better to add a option to change non cast type. It would make this extention more customable. What do you think? Have you time for this?

It's my fault. I should to add collaboration rules in the documentation to save time for developers in your case you need rewrite code to add option.

iamarnas avatar Aug 09 '22 05:08 iamarnas

@tranhuongk What about your extension. Of course you can clone and make your own becouse it's open source. When your have a good product it's not possible avoid dublicates in the market. This extension also based on old and non supporting open source. But if you want make a good product it's better it make with team 😉

iamarnas avatar Aug 09 '22 08:08 iamarnas

@tranhuongk Hi 👋 It's great to see people who want to join and help create a good product together. I'm on vacation right now and don't have a chance to check the code. I want to explain that this generator follow code guide style by Dart team and Dart style. I don't know reason why you want change as default with non cast type. But would be nice to keep default style as Dart team. Maybe it would be better to add a option to change non cast type. It would make this extention more customable. What do you think? Have you time for this?

It's my fault. I should to add collaboration rules in the documentation to save time for developers in your case you need rewrite code to add option.

That is a good plan. I am having some error with code generation, when one of my variables in code generation is returned as an int but the data can also be returned as a double for example. then my model will get error can't assign that variable to int?. above is my whole attempt to get the model to run with whatever data is returned from the api.

tranhuongk avatar Aug 09 '22 09:08 tranhuongk

I am having some error with code generation, when one of my variables in code generation is returned as an int but the data can also be returned as a double for example. then my model will get error can't assign that variable to int?. above is my whole attempt to get the model to run with whatever data is returned from the api.

You should not modify function addType in the ClassDefinition because it is top function, avoid it. I recommend don't touch it. Handle your code as deep as possible before print.

@tranhuongk

iamarnas avatar Aug 09 '22 10:08 iamarnas

@iamarnas yes. Can you update it? It time I so busy with my project. Or I will update and push it after Aug. Thank you!!

tranhuongk avatar Aug 11 '22 16:08 tranhuongk

@iamarnas is this ready to deploy? :)

hiranthaR avatar Apr 17 '23 15:04 hiranthaR

@iamarnas is this ready to deploy? :)

Hi @hiranthaR 👋

No, not really. Here is no changes just type casting in the another way by removing as keyword. The as is considered the safest form of typecasting in dart, and it is used quite often in Flutter UI, particularly when using the bloc library. The current type casting style for this generator is based on Dart team, equal to json_serializable package created by Dart team and I think is best rules to follow theys Dart style. This pull requests woul break Dart team style. Another problem I saw here is removed error check for unimplemented methods where changed to return empty object instead. My opinion is that you can close this pull requests, here is already published dublicate in the marketplace with these changes, by name Json to Dart Model Enhanced.

iamarnas avatar Apr 17 '23 16:04 iamarnas