Thursday 11 May 2023

How to use async method in Flutter Focus onKeyEvent

I have a Flutter app targeting Android TV devices (as well as phones), so I had to implement some special logic to handle the remote.

I wrapped buttons that need to be clicked by a remote with a Focus widget:

 Widget myButton(BuildContext context) {
   Focus(
        onKeyEvent: (node, event) {
          return manageKeyboard(event, context);
        },
          child: IconButton(
      // ... button properties here
    ),
  );
}

I wrote manageKeyboard method like this:

 KeyEventResult manageKeyboard(KeyEvent event, BuildContext context) {
    if (event.logicalKey == LogicalKeyboardKey.arrowRight) {
      if (event is KeyUpEvent) {
        doSomething(context, true);
      }
      return KeyEventResult.handled;
    }
 }

This works reasonably well. Problem is if I try to make managedKeyboard asynchrounous in order to await doSomething, I get below error in onKeyEvent:

The argument type 'Future<KeyEventResult> Function(FocusNode, KeyEvent)' can't be assigned to the parameter type 'KeyEventResult Function(FocusNode, KeyEvent)?'.

How can I make onKeyEvent asynchrounous?



from How to use async method in Flutter Focus onKeyEvent

No comments:

Post a Comment