Home > database >  Why does Flutter's WillPopScope always block going back?
Why does Flutter's WillPopScope always block going back?

Time:02-10

Whenever I've tried to block the ability to swipe back with Flutter on iOS I've tried using WillPopScope. However, regardless of what value I put in onWillPop: it will always block the ability to swipe back.

Why is this? It doesn't matter if onWillPop returns true or false, it always blocks.

    final isConfirmation = _isConfirmation();
    WillPopScope(
      child: Scaffold(
        appBar: _appBar(),
        body: _body(),
      ),
      onWillPop: () async => isConfirmation,
    );

This issue happens even if I return a hard true or false

onWillPop: () async => true

onWillPop: () async => false

onWillPop: () => Future<bool>.value(true)

onWillPop: () => Future<bool>.value(false)

CodePudding user response:

Change your code to this.

if _isConfirmation() is a Future method, use await before it in the code below.

onWillPop: () async {
  final bool confirmation = _isConfirmation();  // use await if it is future.
  return Future<bool>.value(confirmation);
}
  • Related