-
Notifications
You must be signed in to change notification settings - Fork 2
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
Disallow adding non-factory constructors #6
Comments
Hi, The problem is in your class Percentage extends SingleValueObject<InvalidPercentage, ValidPercentage>
with _$Percentage {
Percentage._();
factory Percentage(double value) {
return _$Percentage._create(
value: value,
);
}
/// ⚠️ The problem is here
Percentage.maxed() {
Percentage(1.0);
}
// ...
} Here, you're declaring a named constructor What you want to achieve is not possible, as normally you would need to use a named factory constructor, something like this : factory Percentage.maxed() {
return Percentage(1.0);
} However, named factory constructors are reserved for creating unions of modddels. What I would suggest is to instead use a static method or getter : static Percentage maxed() => Percentage(1.0);
// or
static Percentage get maxed => Percentage(1.0); I think I should completely disallow adding non-factory constructors to a modddel class, as I can't see any valid usecase for them. |
@chikamichi Let me know if this fixed your issue |
Thank you very much! You were right. I was in the process of uncovering the exact stacktrace for I had a feeling
Agreed… until someone finds a legit use-case ;) Maybe just some warning/insight in the doc? |
I've decided to completely disallow non-factory constructors, as they serve no practical purpose and could potentially confuse newcomers. |
Hi,
I have some code available on GitHub showcasing a weird issue.
Context: refactoring a working app to leverage DDD, with proper SoC between:
I got a union of modddels (source) named
MapLayer
, with three unioned "variants":MapLayer.raster
,MapLayer.vector
andMapLayer.geojson
. I applied your insights from #4 and got it working… until I try to get a modddel instance.On this specific call I get the following exception: UnsupportedError (Unsupported operation: It seems like you constructed your class using
MyClass._()
, or you tried to access an instance member from within the annotated class.)My understanding is that it would only ever be raised only if trying to call
MapLayer._()
or an instance method such asmap
on a rawMapLayer
instance, but it’s not what I’m doing here: I’m instantiating through the factoryMapLayer.raster
.What am I missing?
Thank you!
The text was updated successfully, but these errors were encountered: