-1

I want to show PinCode on app resume for all loggedIn Users

so i checked that there is WidgetsBindingObserver

and used it like this :

class ClientApp extends StatefulWidget {
  @override
  ClientAppState createState() => ClientAppState();
}

class ClientAppState extends State<ClientApp>
    with WidgetsBindingObserver {

  @override
  Future didChangeAppLifecycleState(AppLifecycleState state) async {
    if (state == AppLifecycleState.resumed &&
        await auth.isLoggedIn() == true) {
      Navigator.of(context).push(
        MaterialPageRoute(
          builder: (context) => PinCode(),
        ),
      );
    }
    super.didChangeAppLifecycleState(state);
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'App',
      home: SplashScreen(),
    );
  }
}

the problem here that if user places app in memory and get in back (onResume) the event triggers right but i get this error

E/flutter (22097): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Navigator operation requested with a context that does not include a Navigator. E/flutter (22097): The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.

that means that user are not in current state as what i think/understand (correct me if i'm wrong) but how is that ?

yea user is somewhere is application ,, even in splash screen ,, but the same error

Saifallak
  • 1,234
  • 2
  • 13
  • 28

1 Answers1

0

Using natigator Key might help you to push new route instead of build context.

class _AppState extends State<App> {
  final GlobalKey<NavigatorState> navigatorKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      navigatorKey: navigatorKey,
      home: new Scaffold(
        appBar: AppBar(),
        body: new Center(),
      ),
    );
  }
}

Now in the method

 @override
  Future didChangeAppLifecycleState(AppLifecycleState state) async {
    if (state == AppLifecycleState.resumed &&
        await auth.isLoggedIn() == true) {
      Navigator.push(navigatorKey.currentContext,
        MaterialPageRoute(
          builder: (_) => PinCode(),
        ),
      );
    }
    super.didChangeAppLifecycleState(state);
  }

This might help to navigate new page after resuming the app.

Harsha pulikollu
  • 2,386
  • 15
  • 28