Sub topic 15
Common Pitfalls and Version Differences
This chapter assumes that you know Dart classes, named parameters, null safety, basic Flutter structure, and terminal commands. Dart 3 features such as sealed classes are helpful. The examples use freezed, freezed_annotation, json_serializable, and build_runner.
Freezed generates Dart source from annotations. Your source file and the generated files must agree. Most problems come from incorrect part directives, an unfinished generator run, or package versions that do not support the syntax in your example.
Missing or Incorrect Part Files
A regular freezed class normally needs a part directive for its generated implementation:
import 'package:freezed_annotation/freezed_annotation.dart';
part 'user.freezed.dart';
@freezed
abstract class User with _$User {
const factory User({
required String name,
@Default(false) bool active,
}) = _User;
}The part name must match the Dart file name. If this code is in user.dart, the directive must be part 'user.freezed.dart';. The generated file must be in the same directory.
If the class also has JSON methods, add a second part:
part 'user.g.dart';
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);Do not write the generated files by hand. Run the generator instead:
dart run build_runner build --delete-conflicting-outputsThe --delete-conflicting-outputs option removes generated files that prevent a fresh build. Use it for generated output, not for your hand-written Dart files.
Stale Generated Code
The analyzer may report errors such as an undefined copyWith, _$UserFromJson, or _$User. These names come from generated files. They are unavailable until code generation succeeds.
Run the generator after changing:
- A constructor name or field
- A
partdirective - A JSON annotation
- A union constructor
- A package version
For active development, watch for changes:
dart run build_runner watch --delete-conflicting-outputsStop the watcher before changing dependency constraints or diagnosing a confusing build. Then run a clean build and read the first generator error, because later errors may be consequences.
Syntax and Version Differences
Freezed syntax depends on the Dart language version and the installed package versions. A project may use either abstract class or sealed class for its public model, depending on its design and the versions it supports. The important requirements are the @freezed annotation, the generated mixin, the factory constructor, and matching generated output.
Check these files when syntax behaves differently:
pubspec.yamlfor package constraints- The Dart SDK constraint in
environment - The Freezed changelog and documentation for your installed version
- Existing models in the same project
Do not copy generated code from another project. Generated output can change between versions and should be recreated locally.
Collection and Nullability Traps
Freezed makes fields read-only, but a mutable list can still be changed through another reference. Prefer an immutable collection type or avoid exposing mutable collections from your model.
A nullable field is different from a field with a default. String? nickname defaults to null, while @Default('') String nickname is non-nullable and defaults to an empty string. When using copyWith, explicitly test the generated behavior for nullable values and defaults.
A Practical Diagnostic Path
Exercise
Exercise
A model in profile.dart reports that _$Profile is undefined. What should you check first?
Show answer
Check that profile.dart contains part 'profile.freezed.dart';, that the class uses with _$Profile, and that dart run build_runner build --delete-conflicting-outputs completed successfully.