skip to Main Content

How to fix this kind of errors when using then?

void handleSubmitWrapper() {
  final future = handleSubmit();
  future.then(context.loaderOverlay.hide()); // error
}

Future<void> handleSubmit() async {
  ...
}

This expression has a type of ‘void’ so its value can’t be used. Try
checking to see if you’re using the correct API; there might be a
function or call that returns void you didn’t expect. Also check type
parameters and variables which might also be void.dart

2

Answers


  1. You must provide an anonymous function in your case, which has type FutureOr Function(void).

     future.then((void _) => context.loaderOverlay.hide());
    
    Login or Signup to reply.
  2. Since you tagged this dart:async, lets have a look at what you could do when you don’t use that then anachronism:

    Future<void> handleSubmitWrapper() async {
      await handleSubmit();
    
      if (!context.mounted) return;
    
      context.loaderOverlay.hide();
    }
    

    Bonus: you are now able to await this method. Or continue it with a then if you must. Or not. Your choice.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search