flutter - NoSuchMethodError:类'列表' 没有具有匹配参数的实例方法 'cast'
问题描述
我正在尝试从我的 API 服务器获取文章,但NoSuchMethodError: Class 'List<dynamic>' has no instance method 'cast' with matching arguments
出现错误。有谁知道我该如何解决?
List<Article> posts;
final response = await http.get(Uri.parse("$SERVER_IP/api/articles/?format=json"),
headers: <String, String>{"Authorization" : "Token ${globaltoken}"},);
final parsed = jsonDecode(utf8.decode(response.bodyBytes)).cast<String,dynamic>();
posts = parsed.map<Article>((json) => Article.fromJSON(json)).toList();
return posts;
解决方案
首先,您不应该使用 cast(),因为附近的操作(在本例中为 parsed.map)已经为您使用 cast() 并因此转换您想要的类型(文章)。省略 cast<String,dynamic>() 应该可以解决您的错误。
另请参阅飞镖文档:
推荐阅读
- javascript - 如何在 for 语句中执行函数?
- c# - 在 C# 中查找树中的先前节点
- javascript - 如何在 Javascript 中使用表格构建抓取网站
- pcf - 如何避免每天将文件上传到 PCF
- c++ - 为什么c ++将垃圾插入数组?
- java - 获取注释字段的类作为自定义注释的默认值
- python - 尝试了几种方法,但这个值错误不断弹出与 WebDriver 一起运行
- typescript - Office 加载项中的多个不同的 Microsoft Graph API 调用
- java - Java TCP Server 未读取文件内容
- c++ - 在对象中存储具有未知类型参数的回调