Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copyWith on a dynamic field is bad cosntructed #803

Closed
oscarshaitan opened this issue Nov 22, 2022 · 1 comment · Fixed by #808
Closed

copyWith on a dynamic field is bad cosntructed #803

oscarshaitan opened this issue Nov 22, 2022 · 1 comment · Fixed by #808
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@oscarshaitan
Copy link

oscarshaitan commented Nov 22, 2022

Describe the bug
Having an obj with a dynamic field, the copyWith is generated with no replacement with null like other fields
The Objs with other 2 examples field

// coverage:ignore-file
import 'package:freezed_annotation/freezed_annotation.dart';

part 'test_obj.freezed.dart';

part 'test_obj.g.dart';

@freezed
class TestObj with _$TestObj {
  @JsonSerializable(fieldRename: FieldRename.snake)
  const factory TestObj({
    required String stringRequired,
    String? stringNullable,
    @Default('1') String stringWithDefault,
    required int intRequired,
    int? intNullable,
    @Default(1) int intWithDefault,
    required dynamic dynamicRequired,
    dynamic? dynamicNullable,
    @Default(null) dynamic dynamicWithDefault,
  }) = _TestObj;


  factory TestObj.fromJson(Map<String, dynamic> json) => _$TestObjFromJson(json);

}

The copyWith generted

/// @nodoc
class _$TestObjCopyWithImpl<$Res, $Val extends TestObj>
    implements $TestObjCopyWith<$Res> {
  _$TestObjCopyWithImpl(this._value, this._then);

  // ignore: unused_field
  final $Val _value;
  // ignore: unused_field
  final $Res Function($Val) _then;

  @pragma('vm:prefer-inline')
  @override
  $Res call({
    Object? stringRequired = null,
    Object? stringNullable = freezed,
    Object? stringWithDefault = null,
    Object? intRequired = null,
    Object? intNullable = freezed,
    Object? intWithDefault = null,
    Object? dynamicRequired = null,
    Object? dynamicNullable = null,
    Object? dynamicWithDefault = null,
  }) {
    return _then(_value.copyWith(
      stringRequired: null == stringRequired
          ? _value.stringRequired
          : stringRequired // ignore: cast_nullable_to_non_nullable
              as String,
      stringNullable: freezed == stringNullable
          ? _value.stringNullable
          : stringNullable // ignore: cast_nullable_to_non_nullable
              as String?,
      stringWithDefault: null == stringWithDefault
          ? _value.stringWithDefault
          : stringWithDefault // ignore: cast_nullable_to_non_nullable
              as String,
      intRequired: null == intRequired
          ? _value.intRequired
          : intRequired // ignore: cast_nullable_to_non_nullable
              as int,
      intNullable: freezed == intNullable
          ? _value.intNullable
          : intNullable // ignore: cast_nullable_to_non_nullable
              as int?,
      intWithDefault: null == intWithDefault
          ? _value.intWithDefault
          : intWithDefault // ignore: cast_nullable_to_non_nullable
              as int,
      dynamicRequired: null == dynamicRequired
          ? _value.dynamicRequired
          : dynamicRequired // ignore: cast_nullable_to_non_nullable
              as dynamic,
      dynamicNullable: null == dynamicNullable
          ? _value.dynamicNullable
          : dynamicNullable // ignore: cast_nullable_to_non_nullable
              as dynamic?,
      dynamicWithDefault: null == dynamicWithDefault
          ? _value.dynamicWithDefault
          : dynamicWithDefault // ignore: cast_nullable_to_non_nullable
              as dynamic,
    ) as $Val);
  }
}

Expected behavior
the dynamic should be

dynamicNullable: freezed == dynamicNullable
          ? _value.dynamicNullable
          : dynamicNullable // ignore: cast_nullable_to_non_nullable
              as dynamic,

considering that dynamic is already nullable

in consequence, if I want to reset the dynamic field with null is not resetting the field and returns the new object with the old value

@oscarshaitan oscarshaitan added bug Something isn't working needs triage labels Nov 22, 2022
@rrousselGit
Copy link
Owner

It's low priority to me. If you want this fixed, feel free to make a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants