Flutter Code generation

Flutter relies on code generation to generate code for you. This is a very powerful tool that can save you a lot of time.

You will see some class with annotations like @freezed or @JsonSerializable. This means that when you change the class, you must run the build runner to generate the code.

As we use code generation in the whole project. You must run this command to regenerate all the generated code from @freezed or other like json.

Why do we use code generation

For example we use @freezed to generate the toJson, fromJson, copyWith, etc...
Freezed also provides us immutability wich is very important in modern development.

This saves a lot of time.

How to run build runner

dart pub run build_runner build --delete-conflicting-outputs

How to run build runner in watch mode

dart pub run build_runner watch --delete-conflicting-outputs

When to run build runner

You must run build runner when you add a new class with @freezed or @JsonSerializable or you change a class with these annotations. Some other libraries use code generation. Take care of the documentation of the library you use.

Ex of a freezed class:

part 'my_freezed_class.freezed.dart';

class MyFreezedClass with _$MyFreezedClass {
  const factory MyFreezedClass({
    required String name,
    required int age,
  }) = _MyFreezedClass;

For ApparenceKit I tried to make it as simple as possible and not relie to much on code generation.