Dart 为什么关键字是“quot;是";在比较类时未按预期运行?

Dart 为什么关键字是“quot;是";在比较类时未按预期运行?,dart,Dart,我正在dart应用程序中使用redux模式。在reducer中,带有“的if语句是”关键字,用于确定正在传递的操作(以类的形式)根本不起作用 DictionaryState dictionaryReducer(DictionaryState state, dynamic action){ if(action is RequestingDictionaryEntryAction){ // This if statement should be executed but it is n

我正在dart应用程序中使用redux模式。在reducer中,带有
“的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
,相反,代码继续执行,函数不会像预期的那样返回,所以不要过于相信,但问题可能在于参数的“动态”类型导致is运算符在编译时失败。我认为可以通过以下方式解决:

DictionaryState DictionarySeducer(DictionaryState状态,动态操作){
if(action.runtimeType==RequestingDictionaryEntryAction){
返回请求字典入口(状态);
}
if(action.runtimeType==ReceivedDictionaryEntryAction){
返回_receivedDictionaryEntry(状态、操作);
}
返回状态;
}

问题出在我作为
动作传递的参数中。我没有正确地实例化这个类。我传递的是类声明本身,而不是它的一瞬间

final action=RequestingDictionaryEntryAction
而不是

final action=RequestingDictionaryEntryAction()


:D:D

is
操作符检查对象是否是该类型(任何子类)的实例。这很有效。因此,如果您没有得到正确的结果,那么您可能有两个不同的类,名为
RequestingDictionaryEntryAction
。如果您使用非
包:
URI导入包库,则可能会意外发生这种情况。可能是
动态
导致了这种情况,因此在这种情况下,dart无法推断类型。。。