【发布时间】:2021-10-01 13:49:16
【问题描述】:
我正在尝试向我的关注者添加 +1,但我不能,因为我遇到了错误。我已经评论了下面的错误,我正在通过 reso coder 实现颤振 DDD。
@immutable
abstract class ValueObject<T> {
const ValueObject();
Either<ValueFailure<T>, T> get value;
/// throws [UnexpextedValueError] containing the [ValueFailure]
T getOrCrash() {
// id = identity - same as writing (right) => right
return value.fold((f) => throw UnexpectedValueError(f), id);
}
Either<ValueFailure<dynamic>, Unit> get failureOrUnit {
return value.fold(
(l) => left(l),
(r) => right(unit),
);
}
bool isValid() => value.isRight();
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is ValueObject<T> && other.value == value;
}
@override
int get hashCode => value.hashCode;
@override
String toString() => 'Value($value)';
}
class Followers extends ValueObject<int> {
@override
final Either<ValueFailure<int>, int> value;
factory Followers(int input) {
return Followers._(validateintegerNotNegative(input));
}
const Followers._(this.value);
}
final updatedUser =
state.user.copyWith(followers: state.user.followers + 1);
错误:
"message": "没有为类型 'Followers' 定义运算符 '+'。\n尝试定义运算符 '+'。"
【问题讨论】:
-
参数
followers接受什么类型的值? -
很可能
followers接受一个对象,但您传递的是一个整数。可以查一下吗? -
大概你的意思是
Followers(state.users.followers.value.right + 1),或者你需要明确地将operator +添加到Followers类来为你做这件事。 -
Followers 参数只接受整数,但我也试图让它接受像 '+' 或 '-' 这样的运算符。