skip to Main Content

I am getting this warning. I have read the documentation. But my code is returning the true, or false value on some condition. How can I migrate this?

WillPopScope(
            onWillPop: () async {
              if (_actionIcon.icon == Icons.search) {
                return true;
              } else {
                setState(() {
                  _actionIcon = const Icon(
                    Icons.search,
                    color: Colors.white,
                  );
                  _appBarTitle = Text('Heading',
                  _managePayrollBloc.add(_getSearch(""));
                });
                return false;
              }
            },
            child: SomeChild()

2

Answers


  1. Chosen as BEST ANSWER

    I have created a new variable

     bool didPop = true;
    

    and then assigned this to canPop. This value is updated in the if condition.

    PopScope(
                canPop: didPop,
                onPopInvoked: (bool didPop) async {
                  if (_actionIcon.icon == Icons.search) {
                    didPop = true;
                  } else {
                    setState(() {
                      _actionIcon = const Icon(
                        Icons.search,
                        color: Colors.white,
                      );
                      _appBarTitle = Text(LocaleKeys.payroll.tr(),
                          style: const TextStyle(color: Colors.white));
                    });
                    didPop = false;
                  }
                },
                child:SomeChild()
    

  2. You can replace that like this:

    PopScope(
      canPop: _myPopDisableEnableLogic(), // set it true according your app logic - your 1st if condition can be inside this (_actionIcon.icon == Icons.search return true
    
      onPopInvoked: (bool didPop) async {
        // put only else condition part of your code here without returning anything
      },
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search