You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter/Dart泛型函数类型不兼容问题求助

Flutter/Dart类型系统问题求助

我已在Flutter团队仓库提交相关Issue,确认这是Flutter/Dart的类型系统问题,现寻求社区帮助。

复现步骤

  • 运行示例代码仓库中的flutter run命令
  • 项目包含两类相似错误场景,对应两个页面入口按钮
  • 点击按钮进入对应页面即可触发错误

场景一:泛型StatefulWidget问题

创建泛型StatefulWidget TypeErrorWidget<T>,包含IdMapper<T>类型的idMapper属性,其中IdMapper<T>定义为:

typedef IdMapper<T> = String Function(T item);

在State的initState中调用widget.idMapper处理widget.items时,抛出错误:

'(ExampleListItem) => String' is not a subtype of type '(dynamic) => String'

预期应生成List<String>类型的ID列表。临时解决办法是将idMapper通过State构造函数传入。

场景二:Freezed联合类型问题

使用Freezed定义泛型联合类型FreezedExampleType<T extends Object>,包含TypedFnExample<T>类型的callback属性。将泛型实例存入List<FreezedExampleType>后,调用callback时抛出错误:

'(ExampleListItem) => String' is not a subtype of type '(Object) => String'

内容的提问来源于stack exchange,提问作者Victor Rene

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 19:03:30