Flutter - Convert Complex JSON to Dart Classes Easily | JSON Serialization

  Рет қаралды 51,394

RetroPortal Studio

RetroPortal Studio

Күн бұрын

Пікірлер: 74
@storylineteam
@storylineteam 4 жыл бұрын
Great video! As for VsCode users, Dart Data Class Generator Extension is a great choice, as you don't need any dependencies, just right click the class and press generate json data
@Gabeyre
@Gabeyre 4 жыл бұрын
Thanks for mentioning Dart Data Class Generator Extension - the Github ( github.com/bnxm/Dart-Data-Class-Generator ) has nice animation showing you can create a serializable class from bare bones model and a beta json (from APi) direct to serializable model. That sounds exciting !
@mrvfino
@mrvfino 3 жыл бұрын
This saved me after 3 sleepless nights, so thanks! The package docs hasn't really made some of those parts clear, in contrast to yours that's concise and very clear to understand.
@RetroPortalStudio
@RetroPortalStudio 4 жыл бұрын
If you find this video useful, consider Following me on: Twitter: twitter.com/theretroportal Instagram: instagram.com/retroportalstudio LinkedIn: www.linkedin.com/in/parasjainrps Happy Coding! ✌️😁
@megrel5403
@megrel5403 Жыл бұрын
I love you man, you just saved me a headache
@maha2818
@maha2818 2 жыл бұрын
Clear explanation. Awsome work. Thanks a lot.
@ahmedmohamedabdelsamie8384
@ahmedmohamedabdelsamie8384 3 жыл бұрын
deserve more than one like man.
@prakashanuvadia8870
@prakashanuvadia8870 2 жыл бұрын
Thanks it's nice and easy explain. i have one question user upload image file is converted in to Json and get back to user in image this way?
@BillBrutzman
@BillBrutzman 2 жыл бұрын
Very helpful and appreciated. Consider adding _ demo _ actually running the code with mock data.
@SHAFIulBARI
@SHAFIulBARI 4 жыл бұрын
Great solution, I can't thank you enough. Be safe and keep up the good work.
@SerreosBebis
@SerreosBebis 4 жыл бұрын
Precise and to the point. Thank you!
@aldyanimayazar7804
@aldyanimayazar7804 4 жыл бұрын
You just save my day. I wanna learn more .. thank you
@SergeKnysh
@SergeKnysh 4 жыл бұрын
Short and clear - thank you!
@abhishekbhambure5146
@abhishekbhambure5146 3 жыл бұрын
That was a great explanation , It really cleared my all doubts and thanks for creating this awesome video , I read Flutter Complete Reference Book before watching this video , Keep going Bro
@anmarm.9487
@anmarm.9487 4 жыл бұрын
Always enjoy your videos.. thumbs up
@ArpitA5724
@ArpitA5724 4 жыл бұрын
Great solution. Will be trying this on the next project.
@thefamousdjx
@thefamousdjx 2 жыл бұрын
Can you please explain why its necessary to convert the json to a class before using it? Im not an advanced flutter user but in my apps I just use the json as it is after receiving it and then map what I need to my list views
@vibhor691
@vibhor691 2 жыл бұрын
Exactly my question, please answer this someone
@sokhavirithsorn3380
@sokhavirithsorn3380 2 жыл бұрын
​​@UCVNOpQmLyxS_qlqUi9eVFRg Similar question to why we need Typescript when we can already use Javascript just fine. Easy to work in a big group since everything is clearly defined for example what fields does a User contains where you might get confused with only json especially when you need to comeback and debug your app a few months later. Avoid errors for example a field can be null or string if you use json your IDE will suggest you to handle the errors accordingly. Another thing is since everything is clearly typed your IDE will be able to give you auto suggestions which increase your development speed (Very useful in my experience if your json is very complex where I have to go and look back at the json multiple times). You might not notice the problems if you work in small group/alone or haven't maintain the app a few months later. I have maintained others people code and it is always a blessing when the developers have everything clearly typed since it gives me a great sense of what the data looks like . So in short, 1. Easier to work in group, 2. Easier to scale and maintain project in a long run, 3. Help with avoiding errors in production, 4. Auto suggestions during development.
@vibhor691
@vibhor691 2 жыл бұрын
@@sokhavirithsorn3380 thankyou for the beautiful explaination , appreciate it ❤
@marco_di
@marco_di 2 жыл бұрын
Great Tutorial 👍🤩 ... is it also usable with Strapi v4 & GraphQL? The JSON's there are looking like very complex JSON's. ClassUser { Data { id, attributes { firstName, lastName, age ... it seems like user information is in a subclass "attributes", a subclass of the user "id"
@WorstDeveloper
@WorstDeveloper 4 жыл бұрын
I don't like all the boilerplate code for those packages either, but it's a bit better at least. I wonder why Dart needs the boilerplate, I've never had to do that in any other language.
@danarputra9181
@danarputra9181 4 жыл бұрын
Well boilerplate make your program easy to understand
@francisnike7545
@francisnike7545 4 жыл бұрын
@@danarputra9181 I doubt that
@ea8841
@ea8841 2 жыл бұрын
most pathetic language ever
@ghasemsadeghi5175
@ghasemsadeghi5175 4 жыл бұрын
Great explanation. thank you.
@leosconflicts
@leosconflicts 4 жыл бұрын
Nice video, but what happens when a model extends another model? Like User could extends Person, would the serialization work in same way?
@hassanosama357
@hassanosama357 3 жыл бұрын
Thanks, Do this with more briefly too.
@nabil.hamawi
@nabil.hamawi 2 жыл бұрын
can I use it for Image ?
@danielm3977
@danielm3977 3 жыл бұрын
Fantastic!!!!!!👏👏
@plugarutudor-stefan7904
@plugarutudor-stefan7904 3 жыл бұрын
explicitToJson: true --> 2h to find the solucion, THANK YOU
@MahbuburRahman-uc7np
@MahbuburRahman-uc7np 3 жыл бұрын
My class has TImestamp field so .g.dart file is not generating. How can I solve this problem?
@praneethchinna9694
@praneethchinna9694 Жыл бұрын
But I have one doubt how will we test the file or how to write a test case ?
@starphoto69
@starphoto69 3 жыл бұрын
excellent, many tanks!
@algife
@algife 3 жыл бұрын
Solution starts at 3:42
@bhuvanlal7274
@bhuvanlal7274 4 жыл бұрын
What is difference between factory and named constructors ??
@abdullahibnamukta9288
@abdullahibnamukta9288 3 жыл бұрын
How to post in database this nested json ?
@msalih
@msalih 3 жыл бұрын
Great!
@king4053
@king4053 3 жыл бұрын
What if I'm using a model class from a pub.dev package?
@kalpafernando1998
@kalpafernando1998 3 жыл бұрын
thanks man
@storylineteam
@storylineteam 4 жыл бұрын
Also i am trying to find a way around enums... right now i just pas the enum value as a string and deserialize it back using if statement... Do you have any other approach on this?
@Gabeyre
@Gabeyre 4 жыл бұрын
I've never had to pass enums back and forth! Just object, nested objects, or lists of objects and by objects I mean full blown data classes (view models or whatever you wanna call it.) Can you describe a situation where one would pass enum as a parameter to an API method call?
@RetroPortalStudio
@RetroPortalStudio 4 жыл бұрын
Precisely my Question ! an Explaination would be great to solve the issue :)
@storylineteam
@storylineteam 4 жыл бұрын
@@Gabeyre I am creating a pet adoption app for a client, in the Pet model, i have an enum of type PetType that contains dog and cat, then when i retrieve the map from firestore, i use petType: map['petType'] == "PetType.Cat" ? PetType.Cat : PetType.Dog, in the frommap method constructor and "petType': petType.toString()," in the toMap contructor. I know there are lots of other approaches on this, like a PetType base class, but coming from a c# background i am used to Enums as they can help me with abstraction.
@roipeker
@roipeker 4 жыл бұрын
@Storyline Team Games u might have to get redundant on that and rely on a Map, to actually map the strings to enums. Where to put the var? Maybe in the global space or same scope as ur enum... and use “import as “ to make it more “clear”? This is one of the main reasons i don’t use enums.often, I rather a simple good old class with private constructor, and constants that define whatever value/values i like directly using instances of the same class... more encapsulated and versatile.
@Gabeyre
@Gabeyre 4 жыл бұрын
@@storylineteam You are right, I can see the constant conversion from PetType to String (to Firestore) and from String to PetType (from Firestore). This is pretty much how I would do it too if I have to use enum property in a model. And since it s Pet model (where there are many pets, cat, dog, fish, etc where you have to manage many different types) using an enum as the pet type makes a lot of sense.
@zekaidemir9304
@zekaidemir9304 4 жыл бұрын
How can i make php mysql database to json data?
@Gabeyre
@Gabeyre 4 жыл бұрын
I imagine this serialization will become built in to Dart core sooner or later. I use DotNetCore api and I can throw regular c# objects as IActionResult tasks and dotNetCore makes it a json, xml or whatever the httpRequest wants. In Dart/Flutter though, we gotta do this Json serialization setup for every single object. ( I mean this isn't done in javascript - well because it is java script objects - the json - but, Why should dart/Flutter devs have to always do this hand-coding serializtion? If it weren't for json-serializor library mentioned here, I would've quit Flutter before I even began using it.
@storylineteam
@storylineteam 4 жыл бұрын
as a C# dev, i hope google makes some similar improvements in dart... For example the lack of interfaces forces us to use abstract classes in cases where a simple interface can do the job etc
@LucaFang
@LucaFang 4 жыл бұрын
@@storylineteam In Dart classes declaration are interfaces themselfs. You can implement an abstract class as an interface just like you do in Java or C#. Dart just don't have "interface" as keyword. The concept is the same.
@matjazkranjc
@matjazkranjc 2 жыл бұрын
@AMG you should never write your own DTOs if you are communicating with another service. You should generate API which is then used. This generates the model needed and API calls as well. This video is not the best case.
@ballaumer9740
@ballaumer9740 3 жыл бұрын
its fuckin AWESOME
@MMLProfessional
@MMLProfessional 3 жыл бұрын
I tried to apply the tutorial , but the class was not generated and I got these errors error: Target of URI hasn't been generated: 'user.g.dart'. (uri_has_not_been_generated at [yat_flutter_app] lib\user.dart:3) Generator cannot target libraries that have not been migrated to null-safety.
@ea8841
@ea8841 2 жыл бұрын
galti kardia bhai dart chose karke ye av tak null safety issue solve nai hua hai
@ea8841
@ea8841 2 жыл бұрын
sabse bakwas language hai ... java me shift hona hoga lagra
@ea8841
@ea8841 2 жыл бұрын
aur koi reply nai aane wala hai is bande ka
@Procsis1
@Procsis1 2 жыл бұрын
@@ea8841 I can't understand this language, please write with English
@Procsis1
@Procsis1 2 жыл бұрын
@@ea8841 I can't understand this language, please write with English
@funprograms2661
@funprograms2661 3 жыл бұрын
Am shocked!!! Is this even legal?? With just a simple cmd and boom!
@jamesperry4470
@jamesperry4470 3 жыл бұрын
With JSON serialization, you are not converting JSON to dart classes, you are converting JSON to dart objects
@xPerfectyy
@xPerfectyy 4 жыл бұрын
It has limitation, if the api fields differ from your model it will be useless.
@RetroPortalStudio
@RetroPortalStudio 4 жыл бұрын
That in itself is not a good approach but yes.. if that is the case and you are getting api from some third party or something... this won't work !
@xPerfectyy
@xPerfectyy 4 жыл бұрын
Even if you wanted to change the fields manually, i noticed that the file generated, contain a messege indicating that we should not change it, do you have any idea why?
@matjazkranjc
@matjazkranjc 2 жыл бұрын
Thank you for your effort to go into explaining thing. But! If you have existing backend with a lot of endpoints and huge model, this approach is useless. Guys and gals, better invest learning time into creating API client which do everything for you from json schemas. Flutter is a frontend and to use it as such, you should not write and maintain your own DTOs. This work was done by someone else already, programming is all about writing once and then reusing it. This approach in video is not reusing. Cheers
Theme data and color scheme in flutter || Let's Flutter It -12
10:40
Learn useState In 15 Minutes - React Hooks Explained
15:45
Web Dev Simplified
Рет қаралды 1,1 МЛН
Ouch.. 🤕⚽️
00:25
Celine Dept
Рет қаралды 18 МЛН
MY HEIGHT vs MrBEAST CREW 🙈📏
00:22
Celine Dept
Рет қаралды 77 МЛН
Help Me Celebrate! 😍🙏
00:35
Alan Chikin Chow
Рет қаралды 89 МЛН
Seja Gentil com os Pequenos Animais 😿
00:20
Los Wagners
Рет қаралды 39 МЛН
Converting JSON to Dart Classes
13:03
Bleyl Dev
Рет қаралды 22 М.
JSON Serializer in Flutter for better network requests
16:58
Flutter Explained
Рет қаралды 10 М.
Working With JSON In Dart + Flutter - 25 Days Of Flutter
15:22
Benjamin Carlson
Рет қаралды 2,7 М.
This Folder Structure Makes Me 100% More Productive
24:36
Web Dev Simplified
Рет қаралды 43 М.
Top 10 Widgets every Flutter Developer should know!
17:17
RetroPortal Studio
Рет қаралды 282 М.
Display Data From a JSON in Flutter
7:11
Programming Addict
Рет қаралды 125 М.
Ouch.. 🤕⚽️
00:25
Celine Dept
Рет қаралды 18 МЛН