Open Fernandomr88 opened 7 months ago
@goderbauer
It is correct that you can't have a color and decoration on a Container at the same time. Right now we can only detect this situation at runtime. A lint for this would be nice to have. However, this is not the only place in the API where we have mutually exclusive arguments. I would prefer it if we had a generic way of marking mutually exclusive arguments (and linting based on that) instead of implementing individual one-off lints for very specific cases.
It is correct that you can't have a color and decoration on a Container at the same time. Right now we can only detect this situation at runtime. A lint for this would be nice to have. However, this is not the only place in the API where we have mutually exclusive arguments. I would prefer it if we had a generic way of marking mutually exclusive arguments (and linting based on that) instead of implementing individual one-off lints for very specific cases.
That would be much better indeed. Should I change the proposal?
I would prefer it if we had a generic way of marking mutually exclusive arguments ...
The first option that comes to mind (though maybe not the best) would be to define an annotation (like @MutuallyExclusive('group name')
, only better) and allow the analyzer to warn if
A language feature might be nicer, but I don't know whether we could get that. (Also the annotation might be more flexible because any style of parameter could be in the same group.)
A language feature might be nicer, but I don't know whether we could get that. (Also the annotation might be more flexible because any style of parameter could be in the same group.)
Agreed, should I try and propose this on dart-lang/language? I have tried on flutter and was instructed to come here (as mentioned issues above)
If we define an annotation then we'll implement, warning rather than lints, and we can move this issue to the 'sdk' repository. But for now this is a reasonable place for the issue.
💯
I captured some related thoughts in dart-lang/sdk#47712.
Input welcome!
Wow, awesome to see that this is already a work in progress. Thanks @pq
Sure thing. To be clear though, it's just a proposal... Please add any context there. Any additional motivation is greatly valued!
Description
Whenever you're implementing a Container for example and you use both color and decoration at the same time, there's no error shown but an exception is thrown only when the widget is built in runtime, not even on compilation. Code example:
Details
Cannot provide both a color and a decoration. To provide both, use
decoration: BoxDecoration(color: color)
Kind
Guard against runtime errors
Bad Examples
Good Examples
Discussion
This is coming from two previous created issues.
https://github.com/flutter/flutter/issues/133760 https://github.com/flutter/flutter/issues/133456
Discussion checklist
I have come to this error multiple times, only in runtime - not even compile time, when I started to code using Flutter. This could have been prevented by this simple rule.