My app has a websocket backbone, but it's connection is severed whenever the app goes into inactive (or background) for more than 10~ish seconds.
So I decided to make
class MyApp extends StatelessWidget {...}
into
class MyApp extends StatelessWidget with WidgetsBindingObserver {
...
AppLifecycleState state = AppLifecycleState.detached;
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if(state != this.state) {
this.state = state;
if (state == AppLifecycleState.resumed) {
// if websocket is dead, bring it back up
sc.sc.isActive ? debugPrint('is still active') : sc.activate();
}
}
}
@override
Widget build(BuildContext context) {
WidgetBinding.instance.addObserver(this);
...
}
...
}
(where sc is my websocket)
I've implemented the above change and the app still runs fine. But I just wanted to check if this is a feasible way to implement such feature.
I'm new to Fl开发者_如何学编程utter and whenever I make changes like these, I feel pretty lost.
Like when MyApp is yelling at me for using non-final variables within an immutable classes; I mean it doesn't seem so bad or wrong, but I didn't "learn" Flutter so I'm not sure if its "okay" to leave it that way.
Anyways, any input will be appreciated! Thanks in advance.
精彩评论