-
-
Notifications
You must be signed in to change notification settings - Fork 23
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
Can single fields be ignored if not null? #105
Comments
Currently the ignoreNull is only implemented for a single class, not a single field. But it seems like a nice feature to add. |
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 @MappableClass()
class TestState with TestStateMappable {
TestStatus status,
String name,
TestState(this.status, this.name)
@MappableConstructor()
TestState(this.name) : status = TestStatus.initial
} |
Sorry I misread the issue the first time. @2x2xplz is right, ignoring fields is as simple as leaving them out of the constructor. |
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 eg.
Again, this package is great. I think this may be a feature worth considering for the future. Thanks. |
I see. Your right I think this is currently not possible. |
I also came across this situation and wante this feature too. |
I'd like to see this feature too "migrating from Freezed". |
Yes, please, this would be awesome. |
lol I tried to fork and to contribute: results: not my level :) So to share what I got in mind and tried to implement is
Hope someone will be able to be more efficient than me on this repo 😅 |
So good news I finally get time to go deeper on the change. @MappableClass(ignore: ['id'])
class BaseDto with BaseDtoMappable {
BaseDto({
required this.id,
required this.createdAt,
required this.modifiedAt,
});
final String id;
final DateTime createdAt;
final DateTime modifiedAt;
} To test: static void testFun() {
final base = BaseDto(id: '123', createdAt: DateTime.now(), modifiedAt: DateTime.now());
print(base.toJson());
// Results: flutter: {"createdAt":"2024-09-24T16:27:16.591556Z","modifiedAt":"2024-09-24T16:27:16.591556Z"}
} Magic :) are we ready to migrate? |
Thanks for looking into this. I have a few comments: I think this would be nicer as an option on @MappableField(ignore: true) instead of repeating the names on top. How would you expect this to behave for deserialization. In your example the field is required, but shouldn't it be ignored for deserialization too? That would mean only nullable fields or ones with default values can be ignored. If we want to give more granular control, like ignoring only for serializing or deserializing (and not both) how could that look like. Do we want to allow ignoring fields also for toString or equality? I want to avoid adding something to the package that fixes only a specific case but opens up 10 more new ones. |
Thank you for raising these issues.
Yes you're right, here I just tried to manage with a quick win and no breaking change.
The ignore is only for serialization(
In the example I'm receiving fromJson that containts the id of the field (in database) and the object is correct and contains the id (what we expect) const json = '{"id": "123", "createdAt":"2024-09-25T05:19:22.440733Z","modifiedAt":"2024-09-25T05:19:22.440733Z"}';
final fromJson = BaseDtoMapper.fromJson(json);
print(fromJson);
// Results: flutter: BaseDto(id: 123, createdAt: 2024-09-25 05:19:22.440733Z, modifiedAt: 2024-09-25 05:19:22.440733Z)
print(fromJson.toJson());
// Results: flutter: {"createdAt":"2024-09-25T05:19:22.440733Z","modifiedAt":"2024-09-25T05:19:22.440733Z"} Here is the correct behavior, we won't push this information in database, so that's why |
I'm thinking about re-using the Then this could be either excluding specific methods, or including specific methods. |
Yes this is great, I was not aware about this capacity. And your idea is to go from |
EDIT: This does not work actually. Yes, @stact , I agree I would like to see it in an annotation as @schultek suggested. I keep coming back to this issue. Let's say I have a class like: @MappableClass()
class LedgerItem with LedgerItemMappable {
const LedgerItem({
required this.date,
this.amount,
this.balance,
this.note,
}) : computedBalance = null,
assert(amount != null || balance != null,
'Balance must not be null if amount is null');
final DateTime date;
final double? amount;
final double? balance;
final double? computedBalance;
@MappableField(hook: NullEmptyStringMappingHook())
final String? note;
...
} As it is now, I cannot add @MappableConstructor()
const LedgerItem({
...
const LedgerItem.computed({
required this.date,
this.amount,
this.balance,
required double this.computedBalance,
this.note,
}) : assert(amount != null || balance != null,
'Balance must not be null if amount is null');
LedgerItem withComputedBalance(double computedBalance) => LedgerItem.computed(
date: date,
amount: amount,
balance: balance,
computedBalance: computedBalance,
note: note,
); |
Also, could we do something like: @MappableField(serialize: true, deserialize: false) (both would default to true, but this way you can control both directions -- as opposed to |
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 thisIs there an equivalent to
@JsonKey(includeFromJson: false, includeToJson: false)
indart_mappable
?If not, any chance of including that in the future?
The text was updated successfully, but these errors were encountered: