2. First Serializable

You can create serializable classes by annotating the with @serializable and providing an unnamed constructor for all final serializable fields. This must either be the default constructor, or a constructor named dog. Additionally, you should add the self-referencing Dataclass mixin.

@serializable
class Person with Dataclass<Person> {

  String name;
  int age;
  Set<String>? tags;
  
  Person(this.name, this.age, this.tags);
  
}

// Enums can also be serialized!
@serialzable
enum MyEnum {
  a,b,c;
}

For other types of structure definitions, have a look at Conformity.

You don't have to run the build_runner while working on your project, unless you require the direct use of generated classes, such as Builders. But remember you have to run it after you have made changes to the model for them to take effect at runtime. So remember to run build_runner before running the application.

Use dart run build_runner watchfor automatic code generation.

Last updated