Should I Add Generated Dart Files to Git?

Posted on Jun 5, 2019

Packages like json_serializable or build_value facilitate the life of Flutter developers by generating code that is tedious to write, like JSON parsers or object builders.

The way we generate code in Flutter is currently changing as we speak.

Traditionally, what we would do is:

built_value: ^6.6.0
dev_dependencies:
  build_runner: ^1.5.2
  built_value_generator: ^6.6.0
abstract class Message implements Built<Message, MessageBuilder> {
  String get id;
  // etc...
}

flutter packages pub run build_runner build This generates a file with the ending .g.dart.

In the recent versions of Flutter (e.g. master channel) you can run the 4th step with flutter generate or, as I understand, it will run automatically when you run flutter build and flutter run!

For that, you also need to change your pubspec.yaml and use the new builders section:

builders:
  built_value_generator: ^6.6.0

Now the question is: Should I add the .g.dart files to git?

The community is divided on that, so I will expose some pros and cons and let you decide:

Pros of adding generated files to source control:

Cons of adding generated files to source control:

Seems that the pros are clear, you avoid having to generate them each time you pull code changes “just in case” there was a change that affected them.

However, with the new flutter generate that would not be required anymore.

On the other hand, the build speed can also become a problem for you and your team, and you may want to avoid running the generate command all the time.

I think that both options have valid points.

If you decide to not to add them, you can do so by adding this to your .gitignore:

# Generated Dart files
**/*.g.dart

Later, if it becomes a problem for your team, you can add them. It is easier to add them later on that to remove them from source control.

Share Tweet