【发布时间】:2020-08-09 15:51:58
【问题描述】:
我正在寻找一种方法来比较 Dart 中的两种类型,并在它们相同时得到True。
我有一个方法:
SomeItemType exampleMethod<SomeItemType>(Map<String, dynamic> value) {
if (SomeItemType.toString() == 'BuiltMap<String, String>') {
return BuiltMap<String, String>.from(value) as SomeItemType;
}
// (...)
}
其中我必须检查类型参数SomeItemType 是否实际上等于BuiltMap<String, String> 类型。
到目前为止我发现的唯一方法是上面的字符串比较,我认为这不太优雅。
我尝试了以下解决方案,但它们对我不起作用:
SomeItemType is BuiltMap<String, String>
// returns False
SomeItemType == BuiltMap<String, String>
// error: The operator '<' isn't defined for the type 'Type'.
BuiltMap<String, String> is SomeItemType
// error: The operator '<' isn't defined for the type 'Type'.
【问题讨论】:
-
您能否为您的示例添加更多详细信息?如果不知道您如何调用
exampleMethod,就不可能知道问题。
标签: dart