What is the equivalent of Android LiveData in Flutter?
You can use WidgetsBindingObserver to listen to the application state.
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 | class AppLifecycleReactor extends StatefulWidget { const AppLifecycleReactor({ Key key }) : super(key: key); @override _AppLifecycleReactorState createState() => new _AppLifecycleReactorState(); } class _AppLifecycleReactorState extends State<AppLifecycleReactor> with WidgetsBindingObserver { @override void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); } @override void dispose() { WidgetsBinding.instance.removeObserver(this); super.dispose(); } AppLifecycleState _notification; @override void didChangeAppLifecycleState(AppLifecycleState state) { setState(() { _notification = state; }); } @override Widget build(BuildContext context) { return new Text('Last notification: $_notification'); } } |
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.