I'm developing an app with flutter.
All goes fine, but I need something.
I need check if user's token has expired when app comes to foreground.
I get the app state with
didChangeAppLifecycleState(AppLifecycleState state).
An example of my code is this (I can't write the real code because is in my job's computer, and now I'm in home):
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
@override
void initState() {
WidgetsBinding.instance.addObserver(this);
super.initState();
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
void didChangeAppLifecycleState(AppLifecycleState state) {
var isLogged = boolFunctionToCheckLogin();
if (state == AppLifecycleState.resume && isLogged) {
// THIS IS THE PLACE WHEN I TRY TO CODE THE MAGIC
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Tutorial Lifecycle'),
),
body: Center(),
);
}
But, inside of didChangeAppLifecycleState function:
- I have tried to make a showDialog with a Navigator.push to login
- I have tried to make directly a Navigator.push
- I tried to restart the app
All of these have the same problem: context is null or similar.
If I do any of these options directly with button or whatever, works.
But when app comes to foreground, the system says me that context is null or Material
Anybody can help to know how to do it??
Thank you all!!