Skip to content

How would you go about writing an object to json? #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Tom71238 opened this issue Feb 24, 2019 · 1 comment
Closed

How would you go about writing an object to json? #4

Tom71238 opened this issue Feb 24, 2019 · 1 comment

Comments

@Tom71238
Copy link

Tom71238 commented Feb 24, 2019

I followed your steps on the annotation (Weather_tutorial) and I wanted to use that lesson to solve the issue of writing an object to json. I think the your work with annotation/build is well explained in your video(s) but does not address the obj to json issues I am experiencing. After reading/browsing for 3 days on and off I can not find an example that converts an object to a json string. I did read that one has to add the following to the BaseResponse.dart file after the factory(s) statement:
Map<String, dynamic> toJson() => _$BaseResponse(this);
The ..g.. file is created but now I have a usage issue. Everything I have read for the past few days ALWAYS gives fromJson (populate object from json string) vs toJson(object to json string).

This link came close using jsonEncode but the userMap came from a jsonString.
ref: https://flutter.dev/docs/development/data-and-backend/json#consuming-json_serializable-models
capture

any help or refs are welcome !

@tensor-programming
Copy link
Owner

tensor-programming commented May 7, 2019

It wasn't required for this particular example but there is plenty of documentation around the built_value library and its examples.

Even with this example, there are toJson functions which were created in the response.g.dart file. You can just implement them the same way that I added the fromJson functions and then use them as you would with any other toJson function.

Sorry it took me so long to respond, I don't check these repositories.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants