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

Flutter报错:InkWell类未定义call方法,组件调用失败

问题描述

我定义了一个返回InkWell的tile函数:

InkWell tile({String? date}) {
  return InkWell(
    child: SizedBox(
      height: 40,
      child: ListTile(
        title: Text(
          date?? 'Null',
          style: const TextStyle(fontSize: 16),
        ),
      ),
    ),
  );
}

我把tile()传入anotherFile:

randomParameter: anotherFile(tile()), // 可能这里有问题?

在anotherFile中尝试调用tile(date:参数)时出错:

anotherFile({required InkWell tile}){
  const String today = 'Today';
  const String tmrw = 'Tomorrow';
  const String week = 'Next Week';
  ExpansionTile(children: [tile(date: today), tile(date: tmrw), tile(date: week)]); //// 报错!
}

报错信息

Error: The expression doesn't evaluate to a function, so it can't be invoked. The method 'call' isn't defined for the class 'InkWell'.

请问该如何解决此调用错误?

解决方案

核心问题是你传递的是tile()执行后生成的InkWell实例,而非tile函数本身。

当你写tile()时,相当于立刻执行这个函数,得到一个固定的InkWell对象。把这个对象传给anotherFile后,你在里面试图用tile(date: today)的方式调用它——但它已经是个Widget实例了,不是函数,所以触发报错。

具体修改步骤:

  1. 修改anotherFile的参数类型:把参数从接收InkWell实例,改成接收一个返回InkWell的函数,类型声明为InkWell Function({String? date})
  2. 传递参数时直接传函数本身:不要加括号调用tile(),直接传tile

修改后的代码示例:

  • 调用anotherFile的地方:
randomParameter: anotherFile(tile), // 直接传函数,不要加括号执行
  • anotherFile的定义:
anotherFile({required InkWell Function({String? date}) tile}){
  const String today = 'Today';
  const String tmrw = 'Tomorrow';
  const String week = 'Next Week';
  ExpansionTile(children: [tile(date: today), tile(date: tmrw), tile(date: week)]);
}

这样就能正常调用tile函数生成不同参数的InkWell实例了。

内容的提问来源于stack exchange,提问作者Ali Hazem

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 12:27:12