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_builder
provided by the core Dart team.You can use
dart_style
as well to format the output of thecode_builder
results.Here is a simple example of the package in use (from the package's example):
In this example you can use the
dart:io
API to create aFile
and write the output fromanimalClass()
(from the example) to the file:You can use the
File
API to read a.json
from the path, then usejsonDecode
on the contents of the file to access the contents of the JSON config.