Skip to main content

avoid-immediately-invoked-functions

effort: 4m
teams+

Suggests moving functions out instead of using then in immediately invoked function expressions.

Immediately invoked function expressions can easily make code hard to read and understand. Instead, consider moving them out as methods/functions or widgets.

Example

❌ Bad:


Widget build(BuildContext context) {
return Container(
// LINT: Avoid immediately invoked function expressions.
// Try moving this function out as a method or widget.
child: () {
int taskCount = getTasks();
return Text('Tasks remaining: $taskCount');
}(),
);
}

✅ Good:

late int taskCount;

void initState() {
super.initState();

taskCount = getTasks();
}


Widget build(BuildContext context) {
return Container(
child: Text('Tasks remaining: $taskCount'),
);
}