Loading... <div class="tip share">请注意,本文编写于 775 天前,最后修改于 194 天前,其中某些信息可能已经过时。</div> # 前言 在开发过程中,总是会遇到各种各样的问题,有时候用的不多越到后面反而容易忘记,所以在这里记录下我在开发过程中出现的问题,以及我是如何解决他的。 # 类型xxx不是类型强制转换中类型xxx的子类型 <div class="tip inlineBlock warning"> 来自于[StackOverflow](https://stackoverflow.com/questions/60840073/unhandled-exception-type-listdynamic-is-not-a-subtype-of-type-liststring) </div> `Unhandled Exception: type 'Listdynamic' is not a subtype of type 'Liststring' in type cast` 翻译过来:`未处理的异常:类型“List<dynamic>”不是类型强制转换中类型“Liststring>”的子类型` 产生的报错如上,这种情况就是类型不一致导致的。例如一个方法中传入的是List\<dynamic>,但是需要的却是List\<String>类型,从而产生的报错。 有时候我们直接用 `as`就可以进行类型转换了,但却不是每次都可以成功的(且并不建议使用) 我们可以通过两种方法进行转换() ```dart var tempList = List<String>.from(value); ``` ```dart var tempList = value.cast<String>(); ``` 更多对于 `as`, `.retype`,`.cast`的用法可以查看此issue:[Effective Dart: When to use "as", ".retype", ".cast"](https://github.com/dart-lang/site-www/issues/736) Last modification:October 20, 2024 © Allow specification reprint Support Appreciate the author AliPayWeChat Like 2 如果觉得我的文章对你有用,请随意赞赏