-
-
Notifications
You must be signed in to change notification settings - Fork 286
Open
Labels
Description
Describe the bug
Extending a class with a custom constructor always produces the following error:
"E The class MyExtendingModel requested a copyWith implementation, yet the parameter field is not
cloneable."
To Reproduce
- Make a new dart project.
- Add this to pubspec.yaml:
dependencies:
flutter:
sdk: flutter
freezed_annotation: ^3.0.0
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^5.0.0
build_runner: ^2.10.4
freezed: ^3.2.3 paste this in your main.dart:
import 'package:freezed_annotation/freezed_annotation.dart';
part 'main.freezed.dart';
void main() {}
class NonConstConstructor {
// public field to demonstrate this.field` is accessible from the copyWith method
final String field;
NonConstConstructor(this.field) {
print('Non-const constructor called with field: $field');
}
}
// copy with is disabled
@Freezed(copyWith: false)
class MyExtendingModel extends NonConstConstructor with _$MyExtendingModel {
MyExtendingModel(super.field);
}
- run:
dart run build_runner build - output:
Building package executable... (1.5s)
Built build_runner:build_runner.
0s freezed on 1 input: 1 skipped
lib/main.dart freezed
E The class MyExtendingModel requested a copyWith implementation, yet the parameter `field` is not
cloneable.
To fix, either:
- Disable copyWith using @Freezed(copyWith: false)
- Make `field` optional
- Make sure `this.field` is accessible from the copyWith method
package:build_runner_test/main.dart:15:7
╷
15 │ class MyExtendingModel extends NonConstConstructor with _$MyExtendingModel {
│ ^^^^^^^^^^^^^^^^
╵
Log overflowed the console, switching to line-by-line logging.
Failed to build with build_runner/jit in 2s; wrote 0 outputs.
Expected behavior
The field is accessible and also copy with is disabled. This should produce valid code