The issue here is that UnsignedDuration._(Duration.zero) is not a constant expression because it involves a function call (UnsignedDuration._) and accessing a static property (Duration.zero).
In Dart, a constant expression must be evaluatable at compile-time. To make zero a constant, you can define it as a Duration and then create a const constructor for UnsignedDuration that takes a Duration.
Here's an example:
extension type UnsignedDuration(Duration _) implements Duration {
// ...
const UnsignedDuration._(this._);
static const zero = UnsignedDuration._(Duration.zero);
// ...
}
However, this will not work because extension type does not support const constructors.
One possible workaround is to define a static getter that returns UnsignedDuration(Duration.zero):
static UnsignedDuration get zero => UnsignedDuration._(Duration.zero);
This is not a const, but it's a simple and efficient way to provide a static zero property.
Alternatively, you could define a separate constant for the underlying Duration and use that to create the UnsignedDuration:
static const Duration _zeroDuration = Duration.zero;
static UnsignedDuration get zero => UnsignedDuration._(_zeroDuration);
While this is still not a const, it does make the underlying value a compile-time constant.
If you really need a const, you might consider using a different design that doesn't rely on an extension type. For example, you could create a class that wraps a Duration and provides the same interface:
class UnsignedDuration {
final Duration _duration;
const UnsignedDuration(this._duration) : assert(!_duration.isNegative);
// ...
static const zero = UnsignedDuration(Duration.zero);
// ...
}
This design allows you to define const instances, including zero. However, it requires more boilerplate code to implement the Duration interface.