1

I want my app to work offline without a user set, and asking for a login when connectivity is back

en excerpt of the code I'm trying:

class _MyAppState extends State<MyApp> {

  @override
  void initState() {
    super.initState();

    Connectivity().onConnectivityChanged.listen((ConnectivityResult result) =>
        checkConnectivity().then((isOnline) {
          if (isOnline && MyApp.store.state.user == null)
              Navigator.of(context).pushReplacement(
                MaterialPageRoute(builder: (context) => LoginPage()),
              );
        }));
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(          
      routes: {
...

but all I can get is this error:

Unhandled Exception: Navigator operation requested with a context that does not include a Navigator.
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.

I tried to wrap my Navigator call inside a Future.delayed as described here but I got the same error

Doc
  • 5,078
  • 6
  • 52
  • 88
  • where have you declare `MaterialApp` ? – anmol.majhail Mar 01 '19 at 12:48
  • Instead of subscribing to the stream in your initState, use a StreamBuilder widget. StreamBuilder take a stream, and a builder method. The builder method will run when the stream emits a new event and you can show different views based on the latest value from the stream. – Jordan Nelson Mar 01 '19 at 13:15
  • @anmol.majhail just below, i edited the code in the answer – Doc Mar 01 '19 at 13:24
  • @Doc You need to define - `MaterialApp` one level above - `MyApp` - so that `Navigator.of` can work. - https://stackoverflow.com/a/54865428/10269042 – anmol.majhail Mar 01 '19 at 13:26

0 Answers0