使用“cast”方法和“as”关键字进行转换之间的区别

Giu*_*nci 5 dart flutter

as在 Dart 中,使用关键字进行转换和使用方法进行转换有什么区别cast

请参见以下示例:

import 'dart:convert';

class MyClass {
  final int i;
  final String s;

  MyClass({required this.i, required this.s});

  factory MyClass.fromJson(Map<String, dynamic> json) =>
      MyClass(s: json["s"], i: json["i"]);
}

void main() {
  const jsonString = '{"items": [{"i": 0, "s": "foo"}, {"i": 1, "s":"bar"}]}';
  final json = jsonDecode(jsonString);

  final List<MyClass> thisWorks = (json["items"] as List)
      .cast<Map<String, dynamic>>()
      .map(MyClass.fromJson)
      .toList();

  final List<MyClass> thisAlsoWorks = (json["items"] as List)
      .map((json) => MyClass.fromJson(json as Map<String, dynamic>))
      .toList();

  final List<MyClass> thisCrashes =
      (json['items'] as List<Map<String, dynamic>>)
          .map(MyClass.fromJson)
          .toList();
}
Run Code Online (Sandbox Code Playgroud)

最后一次调用(使用 进行强制转换as)会导致异常:type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast

我预计使用 进行强制转换as会像使用该cast方法进行强制转换一样,而不会导致异常。

jam*_*lin 8

as在执行运行时检查后执行强制转换,更改对象的静态(编译时已知)类型。它不影响对象的身份。在集合as上使用(例如、、)会强制转换该集合对象本身,而不是其元素。ListMapSet

集合提供了一种.cast方法,该方法返回集合的新对象(“视图”),该集合as对每个元素执行强制转换。

void main() {
  // `list1` is created as a `List<Object>` but happens to store only ints.
  List<Object> list1 = <Object>[1, 2, 3];

  try {
    // This cast will fail because `list1` is not actually a `List<int>`.
    list1 as List<int>;
  } on TypeError catch (e) {
    print(e); // This gets printed.
  }

  // `list2` is a `List`-like object that downcasts each element of `list1`
  // to an `int`. `list2` is of type `List<int>`.
  //
  // Note that `list2` is not a copy of `list1`; mutations to `list2` will
  // affect `list1`.
  List<int> list2 = list1.cast<int>();
  
  // `list3` is `list2` upcast to a `List<Object>`.  No explicit cast is
  // necessary because if a class `Derived` derives from a class `Base`, then
  // Dart also considers `List<Derived>` to be a derived class of `List<Base>`.
  List<Object> list3 = list2;
  
  // `list4` is `list3` downcast to `List<int>`.  Since `list3` refers to the
  // same object as `list2`, and since `list2`'s actual runtime type is
  // `List<int>`, this cast succeeds (unlike `list1 as List<int>`).
  List<int> list4 = list3 as List<int>;

  print(identical(list1, list2)); // Prints: false
  print(identical(list2, list3)); // Prints: true
  print(identical(list3, list4)); // Prints: true
}
Run Code Online (Sandbox Code Playgroud)