Skip to content

the parameter field is notcloneable false positive #1323

@Lurchfresser

Description

@Lurchfresser

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

  1. Make a new dart project.
  2. 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);
}
  1. run:
    dart run build_runner build
  2. 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

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions