0

在转向空安全之后,我很难了解如何重构我的一些代码。

我有一个看起来像这样的方法

final Reducer<Exception> _errorReducer = combineReducers<Exception>([
  TypedReducer<Exception, ErrorOccurredAction>(_errorOccurredReducer),
  TypedReducer<Exception, ErrorHandledAction>(_errorHandledReducer),
]);

Exception _errorOccurredReducer(Exception _, ErrorOccurredAction action) {
  return action.exception;
}

Exception _errorHandledReducer(Exception _, ErrorHandledAction action) {
  return null;
}

既然error被初始化为null,我怎样才能允许一个null返回能够将它设置回null。

最近介绍了静态类型的工作流程,所以我正在学习。

注意:如果有更好的做法来处理我想要实现的目标,请记住分享它。

4

1 回答 1

1

?如果变量接受,只需在类型后添加一个null


Exception? _errorOccurredReducer(Exception _, ErrorOccurredAction action) {
  return action.exception;
}

Exception? _errorHandledReducer(Exception _, ErrorHandledAction action) {
  return null;
}
于 2021-03-16T11:12:22.107 回答