4

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?

Vaibhav Dhoke
  • 459
  • 6
  • 14
Zoltán Györkei
  • 1,025
  • 5
  • 13
  • 21
  • The question is formulated differently. But the answer is the same. Your wiget instantiating those buttons most likely instantiated `MaterialApp` too right ? It shouldn't – Rémi Rousselet Apr 05 '18 at 08:39
  • Thank you, indeed my problem was the same. Using a Builder to fake a new context for the button solved the problem and now works as intended. Thank you! – Zoltán Györkei Apr 05 '18 at 08:46

0 Answers0