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

Поделиться
HTML-код
  • Опубликовано: 9 май 2020
  • #flutter #json #serialization
    In this Flutter Tutorial, we will be talking a look at using Json Serializable, Build Runner & Flutter Annotation package to auto generate code to easily convert Json Data to Dart Classes. This is a good approach for complex applications where classes and dart object can have too many properties to handle code manually.
    Code (Drive Link): drive.google.com/open?id=1yN1...
    🔥 Auto-Generate Flutter Custom Paint Code TRY THIS FREE TOOL👇:
    👉 Flutter Shape Maker: fluttershapemaker.com
    🔥 Learn About the Most Important Widgets in Flutter 👇:
    👉 Widget Essentials Series: bit.ly/3GpfiYw
    👉🏻 Product Hunt: www.producthunt.com/posts/flu...
    (Your UPVOTE Means a lot! 🙌)
    If you find this video useful consider Supporting @RetroPortal Studio :
    👉 Buy Me a Coffee: www.buymeacoffee.com/theretro...
    👉 Patreon: / retroportalstudio
    Social Links @RetroPortal Studio :
    👉 Twitter: / theretroportal
    👉 Medium: / retroportalstudio
    👉 Instagram: / retroportalstudio
    👉 Patreon: / retroportalstudio
    👉 GitHub: github.com/retroportalstudio
    If you like the tutorial, please hit the LIKE and Subscribe button and also consider supporting me on Patreon or Buy Me a Coffee for More Flutter Tutorial on RetroPortal Studio.
    Thank you for Watching! Happy Coding :)

Комментарии • 74

  • @mrvfino
    @mrvfino 3 года назад +2

    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.

  • @storylineteam
    @storylineteam 4 года назад +27

    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 года назад +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 !

  • @SerreosBebis
    @SerreosBebis 4 года назад +1

    Precise and to the point. Thank you!

  • @SHAFIulBARI
    @SHAFIulBARI 4 года назад +2

    Great solution, I can't thank you enough. Be safe and keep up the good work.

  • @abhishekbhambure5146
    @abhishekbhambure5146 3 года назад +1

    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

  • @SergeKnysh
    @SergeKnysh 4 года назад +2

    Short and clear - thank you!

  • @aldyanimayazar7804
    @aldyanimayazar7804 4 года назад +2

    You just save my day. I wanna learn more .. thank you

  • @ArpitA5724
    @ArpitA5724 4 года назад +1

    Great solution. Will be trying this on the next project.

  • @maha2818
    @maha2818 2 года назад +1

    Clear explanation. Awsome work. Thanks a lot.

  • @anmarm.9487
    @anmarm.9487 4 года назад +1

    Always enjoy your videos.. thumbs up

  • @megrel5403
    @megrel5403 Год назад +1

    I love you man, you just saved me a headache

  • @ghasemsadeghi5175
    @ghasemsadeghi5175 3 года назад +1

    Great explanation. thank you.

  • @danielm3977
    @danielm3977 3 года назад +2

    Fantastic!!!!!!👏👏

  • @ahmedmohamedabdelsamie8384
    @ahmedmohamedabdelsamie8384 3 года назад +1

    deserve more than one like man.

  • @BillBrutzman
    @BillBrutzman 2 года назад +1

    Very helpful and appreciated. Consider adding _ demo _ actually running the code with mock data.

  • @starphoto69
    @starphoto69 3 года назад +1

    excellent, many tanks!

  • @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! ✌️😁

  • @msalih
    @msalih 2 года назад +1

    Great!

  • @kalpafernando1998
    @kalpafernando1998 3 года назад +1

    thanks man

  • @prakashanuvadia8870
    @prakashanuvadia8870 Год назад

    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?

  • @hassanosama357
    @hassanosama357 3 года назад +1

    Thanks,
    Do this with more briefly too.

  • @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?

  • @algife
    @algife 3 года назад +2

    Solution starts at 3:42

  • @plugarutudor-stefan7904
    @plugarutudor-stefan7904 3 года назад +1

    explicitToJson: true --> 2h to find the solucion, THANK YOU

  • @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"

  • @ballaumer9740
    @ballaumer9740 2 года назад +1

    its fuckin AWESOME

  • @nabil.hamawi
    @nabil.hamawi 2 года назад

    can I use it for Image ?

  • @thefamousdjx
    @thefamousdjx 2 года назад +1

    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 года назад +1

      Exactly my question, please answer this someone

    • @sokhavirithsorn3380
      @sokhavirithsorn3380 2 года назад +1

      ​​@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 ❤

  • @bhuvanlal7274
    @bhuvanlal7274 3 года назад

    What is difference between factory and named constructors ??

  • @MahbuburRahman-uc7np
    @MahbuburRahman-uc7np 3 года назад

    My class has TImestamp field so .g.dart file is not generating. How can I solve this problem?

  • @WorstDeveloper
    @WorstDeveloper 4 года назад +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 3 года назад +1

      @@danarputra9181 I doubt that

    • @ea8841
      @ea8841 2 года назад

      most pathetic language ever

  • @zekaidemir9304
    @zekaidemir9304 3 года назад

    How can i make php mysql database to json data?

  • @abdullahibnamukta9288
    @abdullahibnamukta9288 3 года назад

    How to post in database this nested json ?

  • @king4053
    @king4053 3 года назад

    What if I'm using a model class from a pub.dev package?

  • @praneethchinna9694
    @praneethchinna9694 9 месяцев назад

    But I have one doubt how will we test the file or how to write a test case ?

  • @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 года назад +2

      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 года назад +1

      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 года назад +1

      @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.

  • @Gabeyre
    @Gabeyre 4 года назад +3

    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.

  • @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 года назад +1

    Am shocked!!! Is this even legal?? With just a simple cmd and boom!

  • @xPerfectyy
    @xPerfectyy 4 года назад +2

    It has limitation, if the api fields differ from your model it will be useless.

    • @RetroPortalStudio
      @RetroPortalStudio  4 года назад +1

      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?

  • @jamesperry4470
    @jamesperry4470 3 года назад +1

    With JSON serialization, you are not converting JSON to dart classes, you are converting JSON to dart objects

  • @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