Flutter Stream Builder Triggered when Navigator Pop or Push is Called. If you don’t use Provider you can listen to changes in the initState(). But I highly recommend using Provider to separate your Services and Pages. In other words, use the MVVM pattern so your code will be scalable and maintainable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | class LandingScreen extends StatefulWidget { @override _LandingScreenState createState() => _LandingScreenState(); } class _LandingScreenState extends State<LandingScreen> { @override Widget build(BuildContext context) { return SplashView(); } @override void didChangeDependencies() { //we don't have to close or unsubscribe SB Provider.of<AuthService>(context, listen: false).streamAuthServiceState().listen((state){ switch (state) { case AuthServiceState.Starting: print("starting"); break; case AuthServiceState.SignedIn: Navigator.pushReplacementNamed(context, Routes.HOME); break; case AuthServiceState.SignedOut: Navigator.pushReplacementNamed(context, Routes.LOGIN); break; default: Navigator.pushReplacementNamed(context, Routes.LOGIN); } }); super.didChangeDependencies(); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.