gpt4 book ai didi

Dart 'The function isn' t defined' 尽管已定义

转载 作者:IT王子 更新时间:2023-10-29 07:10:07 25 4
gpt4 key购买 nike

如何在 onTap() 中正确访问 _runThisFunction(...)

...

class _DealList extends State<DealList> with AutomaticKeepAliveClientMixin {

void _runThisFunction() async {
print('Run me')
}

@override
Widget build(BuildContext context) {
super.build(context);
return FutureBuilder(
future: _loadingDeals,
builder: (BuildContext context, AsyncSnapshot snapshot) {
return snapshot.connectionState == ConnectionState.done
? RefreshIndicator(
onRefresh: _handleRefresh,
child: ListView.builder(
physics: const AlwaysScrollableScrollPhysics(),
itemCount: snapshot.data['deals'].length,
itemBuilder: (context, index) {
final Map deal = snapshot.data['deals'][index];

return _getDealItem(deal, context);
},
),
)
: Center(
child: CircularProgressIndicator(),
);
},
);
}
}

Container _getDealItem(Map deal, context) {
return new Container(
height: 90.0,
child: Material(
child: InkWell(
child: _getDealRow(deal), // <-- this renders the row with the `deal` object
onTap: () {
// Below call fails
// 'The function isn't defined'
_runThisFunction();

},
),
),
);
}

最佳答案

原因是您超出了范围。
小提示:“函数” 一词始终表示您尝试调用的函数不是 class 的一部分和关键字 "method" 向您表明您尝试调用的函数是某个类的一部分。

在您的例子中,_runThisFunction 定义在 _DealList内部,但您正试图从外部调用它。
您需要将 _getDealItem 移入 _DealList 或将 _runThisFunction 移出。

/// In this case both methods [_runThisFunction()] and [_getDealItem()] are defined inside [_DealList].
class _DealList extends State<DealList> with AutomaticKeepAliveClientMixin {
void _runThisFunction() ...

Container _getDealItem() ...
}

/// In this case both functions are defined globally.
void _runThisFunction() ...

Container _getDealItem() ...

您需要确保您也将相同的逻辑应用于 _getDealRow 和其他嵌套调用。

关于Dart 'The function isn' t defined' 尽管已定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51566282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com