On edit forms, if the user goes back with unsaved changes, ask before throwing them away. One accidental back and everything they typed is gone:
Ask only when something changed. If nothing changed, just go back, a prompt there is annoying. Use the same comparison as in Disable the "Save" button until something changes, one _hasChanges flag powers both.
Name the buttons after what they do, "Discard changes" in red and "Keep editing". Don't use Yes / No / Cancel, the user can't tell what cancels what.
Compose screens, like writing a post or an email, need the same guard, just with different buttons. There the user's work can be kept for later, so offer "Save draft" next to "Discard", like LinkedIn does. And if the app can save drafts by itself, do that and skip the prompt, like Gmail does. The check is simpler there too, in code just check that the field isn't empty.
Two things to know. A custom back button must call Navigator.maybePop, a plain pop skips PopScope. And on iOS the swipe back gesture turns off while canPop is false, the user goes through the back button, that's the price of the guard.