This is a pretty old question, but I thought the following would work:
if (!Navigator.of(context).canPop())
or
if (!Navigator.of(globalKey.currentContext!).canPop()