I recommend you use GetController using Getx Package https://pub.dev/packages/get its very powerfull, and has this setup, use a GetService to store the data and it has onclosed or ondestroy. to run logout when the app is destroyed. check it out.