英文:
Why _Map<String, String> is not in [Map<String, String>, bool]
问题
我需要进行运行时类型检查。
void main() {
final type = [Map<String, String>, bool];
final mapTest = {"A": "B"};
if (type.contains(mapTest.runtimeType)) {
print("是的");
} else {
print("不, ${mapTest.runtimeType} 不在 $type 中");
}
bool boobool = true;
if (type.contains(boobool.runtimeType)) {
print("是的, ${boobool.runtimeType} 在 $type 中");
} else {
print("不, ${mapTest.runtimeType} 不在 $type 中");
}
}
mapTest
应该通过测试。但是它没有。有什么问题?
英文:
I need to do runtime type check.
void main() {
final type = [Map<String, String>, bool];
final mapTest = {"A": "B"};
if (type.contains(mapTest.runtimeType)) {
print("yes");
} else {
print("no, ${mapTest.runtimeType} is not in $type");
}
bool boobool = true;
if (type.contains(boobool.runtimeType)) {
print("yes, ${boobool.runtimeType} is in $type");
} else {
print("no, ${mapTest.runtimeType} is not in $type");
}
}
mapTest
should pass the test. But it doesn't. What's wrong ?
no, _Map<String, String> is not in {Map<String, String>, bool}
yes, bool is in {Map<String, String>, bool}
答案1
得分: 1
-
Map<K, V>
是一个抽象类型。任何对象都不可能具有Map<K, V>
的runtimeType
;任何对象都将是某个具体类型的实例。 -
Type
对象通常仅在精确相同的Type
时相等;它不会检查一个Type
是否表示另一个的子类型。
因此,mapTest.runtimeType
不可能与 Map<K, V>
相等。
您也将无法使用 is
;mapTest is type[0]
是不合法的,因为 is
要求右操作数在静态上是已知的。
最好的做法是,在这种情况下,您可以检查静态类型而不是运行时类型:
/// 在所有非`dynamic`对象上提供 [staticType] 获取器。
extension StaticTypeExtension<T> on T {
/// 返回此对象的静态类型。
///
/// 这可用于报告对象的静态类型(由于推断或处理泛型时可能不太明显),它可能与 [Object.runtimeType] 不同。
Type get staticType => T;
}
void main() {
final type = [Map<String, String>, bool];
final mapTest = {"A": "B"};
if (type.contains(mapTest.staticType)) {
print("yes");
} else {
print("no, ${mapTest.staticType} is not in $type");
}
bool boobool = true;
if (type.contains(boobool.staticType)) {
print("yes, ${boobool.staticType} is in $type");
} else {
print("no, ${mapTest.staticType} is not in $type");
}
}
这将打印:
yes
yes, bool is in [Map<String, String>, bool]
话虽如此,最终您想要做的事情可能相当值得质疑。依赖于特定的 Type
值通常不是一个好主意,因为如前所述,子类型不会自动处理。
英文:
Map<K, V>
is an abstract type. It is impossible for any object to have aruntimeType
ofMap<K, V>
; any object will be an instance of some concrete type.Type
objects usually are equal only for the exact sameType
; it does not check if oneType
represents a subtype of the other.
Consequently, it is impossible for mapTest.runtimeType
to ever compare equal to Map<K, V>
.
You also will not be able to use is
; mapTest is type[0]
is not legal since is
requires that the right operand be statically known.
At best, for this case, you could check for static types instead of runtime types:
/// Provides a [staticType] getter on all non-`dynamic` objects.
extension StaticTypeExtension<T> on T {
/// Returns the static type of this object.
///
/// This can be used to report the static type of an object (which might not
/// always be obvious due to inference or when dealing with generics), which
/// might be different from [Object.runtimeType].
Type get staticType => T;
}
void main() {
final type = [Map<String, String>, bool];
final mapTest = {"A": "B"};
if (type.contains(mapTest.staticType)) {
print("yes");
} else {
print("no, ${mapTest.staticType} is not in $type");
}
bool boobool = true;
if (type.contains(boobool.staticType)) {
print("yes, ${boobool.staticType} is in $type");
} else {
print("no, ${mapTest.staticType} is not in $type");
}
}
which prints:
yes
yes, bool is in [Map<String, String>, bool]
That said, whatever it is you ultimately want to do with this is probably pretty questionable. It's usually not a good idea to rely on specific Type
values because, as mentioned, subtypes won't be automatically handled.
答案2
得分: 0
因为 mapTest
的运行时类型是 _InternalLinkedHashMap<String, String>
,与 Map<String, String>
不同,所以未通过类型检查。
为了通过类型检查,你可以考虑将 mapTest
显式转换为 Map<String, String>
。
final type = [Map<String, String>, bool];
final mapTest = {"A": "B"};
if (type.contains(mapTest.runtimeType) || type.contains(mapTest.runtimeType.toString())) {
print("yes");
} else if (mapTest is Map<String, String> && type.contains(Map<String, String>)) {
print("yes, ${mapTest.runtimeType} is in $type");
} else {
print("no, ${mapTest.runtimeType} is not in $type");
}
英文:
It is because mapTest's
runtime type is _InternalLinkedHashMap<String, String>
, which is different from Map<String, String>
so it fails the type check.
To make the type check pass, you can consider converting the mapTest
to Map<String,String
explicitly.
final type = [Map<String, String>, bool];
final mapTest = {"A": "B"};
if (type.contains(mapTest.runtimeType) || type.contains(mapTest.runtimeType.toString())) {
print("yes");
} else if (mapTest is Map<String, String> && type.contains(Map<String, String>)) {
print("yes, ${mapTest.runtimeType} is in $type");
} else {
print("no, ${mapTest.runtimeType} is not in $type");
}
答案3
得分: 0
Dart通过使用`_Map`类继承的`Map`类的构造函数创建`Map`,并且您直接在`type`中提到了`Map<String, String>`的类型。
因此,我建议您采用在变量`type`中创建`runtimeType`的方式。
```dart
void main() {
final type = [{"A": "B"} .runtimeType, bool];
final mapTest = {"A": "B"} as Map<String, String>;
if (type.contains(mapTest.runtimeType)) {
print("yes");
} else {
print("no, ${mapTest.runtimeType} is not in $type");
}
bool boobool = true;
if (type.contains(boobool.runtimeType)) {
print("yes, ${boobool.runtimeType} is in $type");
} else {
print("no, ${mapTest.runtimeType} is not in $type");
}
}
英文:
Dart create a Map
by using constructor of _Map
which inherited by class Map
, and you directly mentioned type of Map<String, String>
in in type
.
So I suggest you to go with the way of creating a runtimeType
in a variable type
.
void main() {
final type = [{"A": "B"} .runtimeType, bool];
final mapTest = {"A": "B"} as Map<String, String>;
if (type.contains(mapTest.runtimeType)) {
print("yes");
} else {
print("no, ${mapTest.runtimeType} is not in $type");
}
bool boobool = true;
if (type.contains(boobool.runtimeType)) {
print("yes, ${boobool.runtimeType} is in $type");
} else {
print("no, ${mapTest.runtimeType} is not in $type");
}
}
答案4
得分: 0
将以下内容翻译为中文:
各种答案,但我找到了最简单的解决方案。
将
final type = [Map<String, String>, bool];
改为
final type = [Map<String, String>().runtimeType, bool];
似乎实现了预期目的。
是的,_Map<String, String> 在 [_Map<String, String>, bool, List<String>] 中。
英文:
Various answers but I found the simplest solution.
Change
final type = [Map<String, String>, bool];
to
final type = [Map<String, String>().runtimeType, bool];
It seems to fulfill the purpose
yes, _Map<String, String> is in [_Map<String, String>, bool, List<String>]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论