flutter_corelibrary icon indicating copy to clipboard operation
flutter_corelibrary copied to clipboard

Nested hook widgets trigger a `avoid_conditional_hooks` false positive

Open shilangyu opened this issue 4 months ago • 0 comments

The following useState is marked:

class W extends HookWidget {
  const W({super.key});

  @override
  Widget build(BuildContext context) {
    if (Random().nextBool()) {
      return HookBuilder(
        builder: (context) {
          useState(1);
          return const SizedBox();
        },
      );
    }

    return const SizedBox();
  }
}

It shouldn't as it exists in a different hook scope. It happens because when analysing W it looks for all used hooks inside of it. Instead it should stop recursing down once it encounters a new hook scope. This is done in helpers.dart@getAllInnerHookExpressions.

shilangyu avatar Feb 08 '24 10:02 shilangyu