flutterdartdart-async

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


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


Solution

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

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