Dart & Flutter
[Flutter] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<String>?' in type cast
ju_young
2023. 2. 15. 15:55
728x90
이 에러는 아마도 dynamic을 String으로 type cast해달라는 말인 것 같다.
예를 들어 다음과 같은 코드가 있다면 keywords 부분에 에러가 뜬다.
KeywordCategory _$KeywordCategoryFromJson(Map<String, dynamic> json) =>
KeywordCategory(
id: json['id'] as String?,
category: json['category'] as String,
keywords: json['keywords'] as List<String>? ?? [],
);
json['keywords']이 List[dynamic]이기 때문인데 이것을 .cast<String>()으로 type cast해주면 해결된다.
KeywordCategory _$KeywordCategoryFromJson(Map<String, dynamic> json) =>
KeywordCategory(
id: json['id'] as String?,
category: json['category'] as String,
keywords: json['keywords']?.cast<String>() as List<String>? ?? [],
);
[Reference]
https://velog.io/@adbr/Unhandled-Exception-type-Listdynamic-is-not-a-subtype-of-type-ListString
728x90