dart 为什么_Map〈字符串,字符串>不在[Map〈字符串,字符串>,bool]中

2ekbmq32  于 2023-03-10  发布在  其他
关注(0)|答案(4)|浏览(141)

我需要做运行时类型检查。

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应该通过测试,但它没有,出了什么问题?

no, _Map<String, String> is not in {Map<String, String>, bool}
yes, bool is in {Map<String, String>, bool}
fnvucqvd

fnvucqvd1#

  1. Map<K, V>是一个 abstract 类型,任何对象都不可能有Map<K, V>runtimeType;任何对象都是某个“具体”类型的示例。
  2. Type对象通常仅对于完全相同的Type是相等的;它不检查一个X1 M5 N1 X是否表示另一个的子类型。
    因此,mapTest.runtimeType不可能与Map<K, V>相等。
    您也将无法使用is;mapTest is type[0]是不法律的的,因为is要求右操作数是静态已知的。
    在这种情况下,您最多可以检查静态类型而不是运行时类型:
/// 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");
  }
}

其打印:

yes
yes, bool is in [Map<String, String>, bool]

也就是说,无论您最终想对它做什么都可能是非常值得怀疑的,依赖特定的Type值通常不是一个好主意,因为正如前面提到的,子类型不会被自动处理。

jfgube3f

jfgube3f2#

这是因为mapTest's运行时类型是_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");
}
ujv3wf0j

ujv3wf0j3#

Dart使用类Map继承的_Map的构造函数创建Map,您在type中直接提到了Map<String, String>的类型。
因此,我建议您采用在变量type中创建runtimeType的方法。

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");
  }
}
3okqufwl

3okqufwl4#

答案很多但我找到了最简单的解决办法。
变更
final type = [Map<String, String>, bool];

final type = [Map<String, String>().runtimeType, bool];
似乎达到了目的
yes, _Map<String, String> is in [_Map<String, String>, bool, List<String>]

相关问题