我在我的dart应用程序中使用了redux模式。在缩减程序中,带有"is"
关键字的if语句根本不起作用,该语句用来确定传递的是哪个操作(以类的形式)。
DictionaryState dictionaryReducer(DictionaryState state, dynamic action){
if(action is RequestingDictionaryEntryAction){
// This if statement should be executed but it is not.
return _requestingDictionaryEntry(state);
}
if(action is ReceivedDictionaryEntryAction){
return _receivedDictionaryEntry(state, action);
}
return state;
}
在调用dictionaryReducer
时,我传递了一个名为RequestingDictionaryEntryAction
的操作,它没有被识别为RequestingDictionaryEntryAction
,相反,代码继续执行,函数没有按预期返回。
发布于 2019-08-19 02:05:46
我不知道,所以不要太相信,但您的问题可能出在“动态”类型的参数上,导致is操作符在编译时失败。我认为可以使用以下方法解决这个问题:
DictionaryState dictionaryReducer(DictionaryState state, dynamic action){
if(action.runtimeType == RequestingDictionaryEntryAction){
return _requestingDictionaryEntry(state);
}
if(action.runtimeType == ReceivedDictionaryEntryAction){
return _receivedDictionaryEntry(state, action);
}
return state;
}
发布于 2019-08-21 22:06:34
问题出在我作为action
传递的参数中。我没有正确地实例化这个类。我传递的是类声明本身,而不是它的一瞬间。
final action = RequestingDictionaryEntryAction
代替
final action = RequestingDictionaryEntryAction();
:D :D
https://stackoverflow.com/questions/57493811
复制相似问题