dart_mappable icon indicating copy to clipboard operation
dart_mappable copied to clipboard

Can single fields be ignored if not null?

Open BlackCatDev-IO opened this issue 2 years ago • 16 comments
trafficstars

Hi, thanks for the work on this package. Its really nice to have the freezed functionality with a much simpler syntax.

I see the ignoreNull option, but one thing I didn't see in the docs is if its possible to ignore a single field even if it isn't null?

The json_annotation includes the option @JsonKey(includeFromJson: false, includeToJson: false) to omit that value from all serialization attempts whether its null or not.

A freezed example of that use case would look like this

enum TestStatus {
  initial,
  loading,
  success,
  failure,
}

@freezed
class TestState with _$TestState {
  factory TestState({
    @JsonKey(includeFromJson: false, includeToJson: false) // this is what I'm specifically referring to
    @Default(TestStatus.initial) TestStatus status,
    @Default('')
    String name,
  }) = _TestState;

  factory TestState.fromJson(Map<String, dynamic> json) =>
      _$TestStateFromJson(json);
}

Is there an equivalent to @JsonKey(includeFromJson: false, includeToJson: false) in dart_mappable?

If not, any chance of including that in the future?

BlackCatDev-IO avatar Jun 18 '23 16:06 BlackCatDev-IO

Currently the ignoreNull is only implemented for a single class, not a single field.

But it seems like a nice feature to add.

schultek avatar Jun 19 '23 10:06 schultek

I'm new to this package so take this with a grain of salt, however this page explains the philosophy: When analysing your code, dart_mappable never looks at the fields of your model, but rather only at the constructor arguments.

Therefore your solution should be to create a TestState constructor that doesn't include the ignored field, and annotate that constructor with @MappableConstructor() Something like:

@MappableClass()
class TestState with TestStateMappable {
  TestStatus status,
  String name,

  TestState(this.status, this.name)

  @MappableConstructor()
  TestState(this.name) : status = TestStatus.initial
}

2x2xplz avatar Jun 25 '23 00:06 2x2xplz

Sorry I misread the issue the first time. @2x2xplz is right, ignoring fields is as simple as leaving them out of the constructor.

schultek avatar Jun 30 '23 15:06 schultek

Sorry, I should have been more clear when I opened the issue. The example I provided only ignores that field for json serialization, but still includes that field in copyWith, and equality comparisons etc...After a quick test, it appears you lose all of that when leaving it out of the constructor with DartMappable. So, that is not the same functionality as the freezed example I provided.

Right now I can get around it in my use case with HydradedBloc by just specifying whatever I want the initial value to be in the fromJson overrides.

eg.

/// this assumes usage of HydradedBloc
...
  @override
  TestState? fromJson(Map<String, dynamic> json) {
    // with freezed this copyWith isn't necessary here when `includeFromJson` is set to false
    return TestState.fromMap(json).copyWith(status: TestStatus.initial); 
  }

Again, this package is great. I think this may be a feature worth considering for the future. Thanks.

BlackCatDev-IO avatar Jul 04 '23 06:07 BlackCatDev-IO

I see. Your right I think this is currently not possible.

schultek avatar Jul 04 '23 06:07 schultek

I also came across this situation and wante this feature too.

boostmerlin avatar Sep 14 '23 09:09 boostmerlin

I'd like to see this feature too "migrating from Freezed".

AhmedLSayed9 avatar Oct 03 '23 13:10 AhmedLSayed9

Yes, please, this would be awesome. Something like a @MappableIgnore() annotation (or even the more esoteric Java-like name, @MappableTransient() -- but I prefer "ignore" as it seems more user-friendly) on the field. Most other serialization libraries offer this. (Also, I really like that this library tries to play nice and not conflict with names in other libraries by using the @Mappable... prefix. It makes it really easy to swap in/out database packages.)

tgrushka avatar Jun 30 '24 04:06 tgrushka