I'm trying to create a code generator that takes input a JSON file and generates multiple classes in multiple files.
And my question is, is it possible to create multiple files for one input using build from dart lang?
I'm trying to create a code generator that takes input a JSON file and generates multiple classes in multiple files.
And my question is, is it possible to create multiple files for one input using build from dart lang?
Copyright © 2021 Jogjafile Inc.
Yes it is possible. There are currently many tools in available on pub.dev that have code generation. For creating a simple custom code generator, check out the package
code_builderprovided by the core Dart team.You can use
dart_styleas well to format the output of thecode_builderresults.Here is a simple example of the package in use (from the package's example):
In this example you can use the
dart:ioAPI to create aFileand write the output fromanimalClass()(from the example) to the file:You can use the
FileAPI to read a.jsonfrom the path, then usejsonDecodeon the contents of the file to access the contents of the JSON config.