0

I am trying to change screen on push button. But somehow the context has no Navigator as the error suggests:

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. When the exception was thrown, this was the stack: #0 Navigator.of.<anonymous closure> (package:flutter/src/widgets/navigator.dart:2554:9) #1 Navigator.of (package:flutter/src/widgets/navigator.dart:2561:6) #2 _MyAppState.build.<anonymous closure>.<anonymous closure> (package:base_app/main.dart:64:35) #3 _InkResponseState.handleTap (package:flutter/src/material/ink_well.dart:1072:21) #4 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:253:24) #5 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:627:11) #6 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:306:5) #7 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:239:7) 

However, according to this message there is a Navigator in #0 and #1 so what is the problem?

Mycode:

import 'package:amplify_auth_cognito/amplify_auth_cognito.dart'; import 'package:amplify_authenticator/amplify_authenticator.dart'; import 'package:amplify_flutter/amplify_flutter.dart'; import 'package:flutter/material.dart'; import 'package:base_app/add_device.dart'; import 'amplifyconfiguration.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key); @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { bool _isAmplifyConfigured = false; @override void initState() { super.initState(); _configureAmplify(); } void _configureAmplify() async { try { await Amplify.addPlugin(AmplifyAuthCognito()); await Amplify.configure(amplifyconfig); setState(() => _isAmplifyConfigured = true); print('Successfully configured'); } on Exception catch (e) { print('Error configuring Amplify: $e'); } } Future<bool> isUserSignedIn() async { final result = await Amplify.Auth.fetchAuthSession(); return result.isSignedIn; } @override Widget build(BuildContext context) { return Authenticator( child: MaterialApp( builder: Authenticator.builder(), home: _isAmplifyConfigured? FutureBuilder( future: isUserSignedIn(), builder: (BuildContext ctx, AsyncSnapshot snapshot) { if (snapshot.data == null) { return const Center( child: CircularProgressIndicator(), ); } else { return Scaffold( floatingActionButton: FloatingActionButton( backgroundColor: Colors.blue, foregroundColor: Colors.white, onPressed: () => { Navigator.of(context).push( MaterialPageRoute(builder: (context) => AddDevice())), }, child: Icon(Icons.add), ) ); } }): Text("not logged in") ), ); } } 

This is a basic code of a more complex app but even here it does not work. I tried several suggestions from other posts with all the same result.

1 Answer 1

1

This error happened because you use a context that is a parent of MaterialApp, try use builder's context which is ctx, like this:

 Scaffold( floatingActionButton: FloatingActionButton( backgroundColor: Colors.blue, foregroundColor: Colors.white, onPressed: () => { Navigator.of(ctx).push( MaterialPageRoute(builder: (context) => AddDevice())), }, child: Icon(Icons.add), ) ) 
Sign up to request clarification or add additional context in comments.

2 Comments

And how to close the drawer after returning from the AddDevice(), neither Navigator.of(ctx).pop() nor Navigator.pop(context) or any other combination with ctx and context work.
drawer? I can't see any code related to drawer in your code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.