-
Notifications
You must be signed in to change notification settings - Fork 217
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
State.copyWith not ideal when it comes to nulls #97
Comments
It certainly isn't! For that type of thing, I almost always use the |
@brianegan From what I understand, Optional it similar to my Nullable, right? I wish there were a cleaner way. For example, with C# I use similar approach, but C# lets you implement implicit cast operators. IOW you would still pass i.e. String, and copyWith with implicitly create Nullable(value) for you. |
Yep, it's pretty much the same thing, but has the extra information like |
Yeah, I agree. BTW, if you are curious about Param<T> c# implementation. |
Hey @MihaMarkic -- can I provide any more help on this one? I'll close it for now since it's a bit stale, but please feel free to reopen! |
No, no further help needed, thanks. |
Is it resolved by dart nullability support? I still can't upgrade all my packages to check |
What about initialize the values and then compare? Like this:
Basically Strings are initialized as empty strings, ints as zero for its initial value, bools could be false or true depending on the use case, and so on... at the end we'll simply need the ternary operator to make it work. I'm a total noob of dart, so i'd ask... is this approach ok? |
@NaturalDevCR there are problems with these "sentinel values" if, for example, you ever want to set I'm using the Optional package to work around the problem, so the code looks something like this:
In this example, duration is a nullable field, and the copyWith pattern works as normal. The only thing you have to do differently is if you are setting
Or if you want to set duration to null:
Alternatively, there are code-gen solutions which handle this problem, because they provide automatically generated methods for copying new instances with changed values. For example, built_value and freezed |
Hi guys, not strictly related to this library, but nonetheless important for redux concept.
The current samples shows state.copyWith something like:
Obviously this code won't work as expected when calling state.copyWith(text: null) where I'd expect that text property becomes null.
One way to solve passing nulls is to wrap values in a i.e. Nullable class:
and then call state.copyWith(Nullable.fromNull()) when setting text to null, or just state.copyWith(Nullable("some text")) when setting to a certain value.
The downside of this solution is that it creates more verbose code and it consumes more memory as everything is wrapped into a nullable.
Alternative solution would be to allow passing of additional bool properties, i.e.
This might be better alternative, but it might be more confusing.
What do you guys think?
The text was updated successfully, but these errors were encountered: