I want to make a flutter app that uses hero widget and inkwell widget, but when I try to use navigator.push to navigate to a new page I get this error:
Compiler message: lib/main.dart:238:25: Error: Getter not found: 'context'.
Navigator.push(context ,MaterialPageRoute(
^^^^^^^
I try to ask my question in some Telegram group and they answer me that I should define name ‘context’, but when I do it, I get this error and even navigator doesn’t push to the new page.
...
Widget _buildPillItmes(String name, String imagePath, String count){
return Padding(
padding: EdgeInsets.only(top: 20.0, right: 10.0, left: 10.0),
child: new InkWell(
onTap: () {
//BuildContext context;
Navigator.push(context ,MaterialPageRoute(
builder: (context) => DetailsPage(heroTag: imagePath, pillName: name, pillCount: count)
));
},
child: new Row(
...
I expected to navigate to the new page but I get nothing except my first page
3
Answers
you need to have context in scope, if you just define the name it will be null when you use it, in your main app you have a build method that takes the context as a parameter, so you can do something like this:
you just need to adapt this to your case and find the closest point where you have the context, probably you have it where you call _buildPillItmes
i think they say like a param:
Hope i thelps.
You have a method that returns a Widget. So I expect you are calling this method in your main widget’s build method, right?
So, in this method’s scope you don’t have “context”, you should pass it as a parameter. You only have the context inside build method.
In your main widget:
Your method