1

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!!

1 Answer 1

1

I've solve it.

Into the conditional mark with "magic", I've call "showDialog" inside of setState:

void didChangeAppLifecycleState(AppLifecycleState state) {
    var isLogged = boolFunctionToCheckLogin();

    if (state == AppLifecycleState.resume && isLogged) {

        setState(() {
            showDialog(
            context: context,
            builder: (BuildContext context) {
                ... blablabla ...
            }
        });

    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.