Instead of writing code yourself, you can always use generators to create Dart model classes from JSON. Here are the results of my evaluation of some free tools for you.
For this test, I created a sample JSON structure that all tools need to deal with. You can find it below:
Every converter will receive the same input and I’ll examine the created output. Is it runnable? Is null-safety supported? Is the code efficient? Is anything missing? Have a look at the results below.
I started an internet search and took the first results I found. The list of participants are
Go with Dart QuickType. It generates the best Dart code from JSON objects.
Quicktype is a multi-language converter for JSON objects and it also supports Dart. There are options to customize the generated class. And there is also an extension for Visual Studio Code.
The result contains everything, but there are problems:
- Quicktype doesn’t use the
requiredkeyword, but the old
- Quicktype doesn’t support null-safety
This makes the code unusable without manual changes. There are also 77 open issues for the Dart language and the official source code hasn’t been updated for months. I wouldn’t expect any progress here, but the tool still saves development time despite the necessary manual changes.
Solid, but not perfect. Faster than writing by yourself. ?
JSON to Dart Converter
JSON to Dart Converter is a free online Dart model class generator. The only available option is to make fields private. In that case, getters and setters are generated.
The JSON can be converted into a Dart class but with major problems:
- No null-safety
- Getters and setters are generated for fields when they are private
- Array was not recognized as
List<int>, but as
- List of type
Objectwas not recognized
Overall the code is pretty much useless because you’ll need so much time fixing the errors that you are faster if you write it by yourself. There is no GitHub repository or anything similar, just a mail address for contact. The website looks like it started yesterday and there is no content except this converter.
Don’t use it, it’s a waste of time! ⛔
JSON formatter is another free online Dart model class generator. There are no options to select, you just input the JSON and the tool converts it into a Dart class.
The output is useless, there aren’t even
toJson methods included. No null-safety, no final fields, but at least the object structure was converted correctly.
Absolutely not worth it! ⛔
JSON to Dart Model
JSON to Dart Model provides many customization options for the output. The code is open-source and there are currently 3 open issues. The project doesn’t seem to be as known as quicktype, but the generated code will work out of the box with the current Dart version.
It’s a bit annoying that the converted JSON snippets are included in the code as comments. Also, the conversion of arrays seems very cumbersome. And sadly, the converter created two identical objects with different names. But apart from that, it looks good. However, I wouldn’t use the result as the code style is very odd and I’d have to do a lot of refactoring.
Working and usable, but odd code style. ?
Dart QuickType is a fork of Quicktype with a much more recent code base. All of the mentioned flaws of Quicktype were fixed here. There are no open issues and the created Dart code looks simple and slim.
Default values, null-safety, final fields … everything you need! The only little thing that I would like to have added is equatable because I use it all the time. But I can tolerate this one manual step. Maybe the author works on my proposal for a future release. ?
Update: equatable was added in a recent release!
Clear winner, no objections, best code generation! ✔
JSON to Dart online converter Null Safety
JSON to Dart online converter Null Safety has no options except for setting the class name and the generated code does not work out of the box.
Some flaws with this code:
latekeyword when using
- Variables can be named like keywords → compiler error
- Creates two identical classes
- No fallback values in the
- Additional compiler errors with created classes
Too many manual corrections needed to be useful. ⛔
Dart QuickType generates the best code and is the winner of this comparison. To some extent, you can also use JSON to Dart Model if you don’t mind the code style. All other converters are not worth the time, so just skip them.
Did I miss a good converter? Tell me about it if you think I should give it a shot ?