skip to Main Content

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


  1. 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:

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Welcome to Flutter',
          home: Scaffold(
            appBar: AppBar(
              title: Text('Welcome to Flutter'),
            ),
            body: _buildPillItmes("name", "image", "count", context),
          ),
        );
      }
    
    
      Widget _buildPillItmes(String name, String imagePath, String count, BuildContext context){
        return Padding(
          padding: EdgeInsets.only(top: 20.0, right: 10.0, left: 10.0),
          child: new InkWell(
           onTap: () {
             Navigator.push(context ,MaterialPageRoute(
                 builder: (context) => DetailsPage(heroTag: imagePath, pillName: name, pillCount: count)
             ));
           },
          child: new Row(
       ...
    }
    

    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

    Login or Signup to reply.
  2. i think they say like a param:

    ...
    Widget _buildPillItmes(BuildContext context, String name, String imagePath, String count){
      return Padding(
        padding: EdgeInsets.only(top: 20.0, right: 10.0, left: 10.0),
        child: new InkWell(
           onTap: () {
             Navigator.push(context ,MaterialPageRoute(
                 builder: (context) => DetailsPage(heroTag: imagePath, pillName: name, pillCount: count)
             ));
           },
          child: new Row(
    ...
    

    Hope i thelps.

    Login or Signup to reply.
  3. 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:

    class Main extends StatelessWidget {
      @override
      Widget build(BuildContext context) { // Here you have the context
        return new Center (
          child: this._buildPillItmes("name", "imagePath", 0, context), // Here you pass the context
        );
      }
    }
    

    Your method

    ...
    Widget _buildPillItmes(String name, String imagePath, String count, BuildContext context){ // Here you expect the context
      return Padding(
        padding: EdgeInsets.only(top: 20.0, right: 10.0, left: 10.0),
        child: new InkWell(
           onTap: () {
             // Now you have context and it should work
             Navigator.push(context ,MaterialPageRoute(
                 builder: (context) => DetailsPage(heroTag: imagePath, pillName: name, pillCount: count)
             ));
           },
          child: new Row(
    ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search