I have the following structure in my app:
Main Page - Navigation Drawer -> Navigator.push opens new Scaffolds
However, after I navigate to one of the pages via the navigation drawer menu points, where I have further buttons that would open up another page, I get the following error:
I/flutter ( 6391): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/flutter ( 6391): The following NoSuchMethodError was thrown while handling a gesture:
I/flutter ( 6391): The method 'ancestorStateOfType' was called on null.
I/flutter ( 6391): Receiver: null
I/flutter ( 6391): Tried calling: ancestorStateOfType(Instance of 'TypeMatcher<NavigatorState>')
I/flutter ( 6391):
I/flutter ( 6391): When the exception was thrown, this was the stack:
I/flutter ( 6391): #0 Object.noSuchMethod (dart:core-patch/dart:core/object_patch.dart:46)
I/flutter ( 6391): #1 Navigator.of (package:flutter/src/widgets/navigator.dart:722:19)
I/flutter ( 6391): #2 Navigator.push (package:flutter/src/widgets/navigator.dart:557:22)
I/flutter ( 6391): #3 buildBasicWidget.<anonymous closure> (package:osszefogasaszanhuzokert/dog.dart:388:43)
The part where the error is coming from is a pretty simple FlatButton:
new Row(
mainAxisAlignment: MainAxisAlignment.end,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
new Container(
padding: const EdgeInsets.only(top: 43.0),
child: new RaisedButton(
color: Colors.blueGrey,
onPressed: (){
Navigator.push(context, new MaterialPageRoute(
builder: (context) => new DogDetails(URL)));
},
child: new Text('Részletek')),
),
The purpose of this button is to open up the new page where there is a detailed view of all the information about the dog. The page I'm trying to navigate from is just a list with some basic information about every dog.
But I don't understand why do I get this error (it also happens on another page where I just want to open the image for full view when the user clicks on it). My guess is that the problem lies here:
Navigator.push(context,
And somehow I pass the context in a faulty way. Currently, I just initialized a context variable in the build process of the widget (that contains the button that opens up the detailed page, these widgets are displayed using a ListView.builder).
BuildContext context;
How should I exactly pass the context to the Navigator.push
a method in order for the push to work?