Malak
Malak

Reputation: 461

context problem in flutter "Undefined name 'context'"

Is someone able to explain to me why 'context' in there is undefined? i Watched like 5 videos about BuildContext and i still don't understand it. Yes i'm beginner with dart

class ZmienneClass extends ChangeNotifier {
void decrementCounter(int liczba) {
      if (_rundy == 0) {
        Navigator.push(context,
            MaterialPageRoute(builder: (context) => resGamePage(title: "")));
      };}}

Upvotes: 1

Views: 2245

Answers (2)

Ravindra S. Patil
Ravindra S. Patil

Reputation: 14885

You dont declare BuildContext try below code refer BuildContext here

class ZmienneClass extends ChangeNotifier {

void decrementCounter(int liczba,BuildContext context) {
      if (_rundy == 0) {
        Navigator.push(context,
            MaterialPageRoute(builder: (context) => resGamePage(title: "")));
      };}}

Upvotes: 3

esentis
esentis

Reputation: 4726

The BuildContext is needed to tell Flutter where it should build its widgets. Currently you are trying to access a context without actually providing a valid BuildContext, if you really want to access the context, you could provide it trough the decrementCounter by passing one more parameter in it, decrementCounter(int liczba, BuildContext context) and pass the context from where you are calling it.

Upvotes: 3

Related Questions