'How to make a widget require one of two properties?
So I have a widget that can accept a color or a gradient property :
class OuterWheel extends StatelessWidget {
final double outerRadius;
final double innerRadius;
final TextStyle textStyle;
final double percentage;
final int decimals;
final Color? color;
final List<Color>? gradient;
const OuterWheel({
Key? key,
required this.outerRadius,
required this.innerRadius,
required this.textStyle,
required this.percentage,
required this.decimals,
this.color,
this.gradient,
}) : super(key: key);
I would like to make it like in TypeScript where you could have a union type like Color|List<Color> , so I could only use one property, but apparently we don't have that in Dart.
So how can I make so that you have to choose one of these two fields?
Solution 1:[1]
What about mutual exclusive operator?
const OuterWheel({
Key? key,
required this.outerRadius,
required this.innerRadius,
required this.textStyle,
required this.percentage,
required this.decimals,
this.color,
this.gradient,
}) : assert((color == null) ^ (gradient == null), 'color and gradient are mutually exclusive'), super(key: key);
This way you ensure that only one of them, but not both, is not null.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | GGirotto |
