The value of expression of type 'void' can't be used in then()

620 Views Asked by At

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

There are 2 best solutions below

0
Vladimir Gadzhov On BEST ANSWER

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

 future.then((void _) => context.loaderOverlay.hide());
2
nvoigt On

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.