Search code examples
flutterdartnavigation

"Undefined name 'context'. Try correcting the name to one that is defined, or defining the name." Flutter


This is a snippet of my widgets.dart file where I defined a widget called see_all_cards and its only purpose is to show an extended list of all cards that I was initially displaying. It should just redirect to Trending.dart. That's my main goal here.

Widget see_all_cards(){
  return  Container(
        child: FlatButton(
                  child: Text(
                    "See all (43)",               
                    style: TextStyle(
                      color: Theme.of(context).accentColor, // error
                    ),
                    ),

                  onPressed: (){
                    Navigator.push(
                      context,  // error
                      MaterialPageRoute(
                        builder: (BuildContext context){
                          return trending();     
                        },
                      ),
                    );
                  },       
        )
  );
}

The following segment is my main page. I've called SlowlyApp from void main.

class SlowlyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'SlowlyApp',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Search',
            style: TextStyle(
              color: Color.fromRGBO(0,0,0,1),
            ),
          ),
          backgroundColor: Color.fromRGBO(225,225,0,1),         
          actions: <Widget>[
            IconButton(
              icon: 
                Icon(Icons.search), 
                onPressed: (){
                  showSearch(context: context, delegate: data_search());
                }
            ),
          ],
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[       
            smallgap(), 
            current_cards_heading(),
            current_cards(),
            see_all_cards(),
            smallgap(),
          ],
        ),
      ),
    );   
  }
}

Solution

  • see_all_cards should expect context as parameter. You only have context in your main widget's build method

    Widget see_all_cards(BuildContext context){
      return  Container(
            child: FlatButton(
                      child: Text(
                        "See all (43)",               
                        style: TextStyle(
                          color: Theme.of(context).accentColor, // error
                        ),
                        ),
    
                      onPressed: (){
                        Navigator.push(
                          context,  // error
                          MaterialPageRoute(
                            builder: (BuildContext context){
                              return trending();     
                            },
                          ),
                        );
                      },       
            )
      );
    }
    

    And then you can call passing the context.

    class SlowlyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'SlowlyApp',
          home: Scaffold(
            appBar: AppBar(
              title: Text('Search',
                style: TextStyle(
                  color: Color.fromRGBO(0,0,0,1),
                ),
              ),
              backgroundColor: Color.fromRGBO(225,225,0,1),         
              actions: <Widget>[
                IconButton(
                  icon: 
                    Icon(Icons.search), 
                    onPressed: (){
                      showSearch(context: context, delegate: data_search());
                    }
                ),
              ],
            ),
            body: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[       
                smallgap(), 
                current_cards_heading(),
                current_cards(),
                see_all_cards(context),
                smallgap(),
              ],
            ),
          ),
        );   
      }
    }