Home > Software engineering >  Can't access variable in Flutter GetX
Can't access variable in Flutter GetX

Time:06-21

I am trying to pull the variable named _pageIndex from a file called GetX welcomeMain.dart and change its value.

welcomeMain.dart:

class getPageIndex extends GetxController {
  int _pageIndex = 0;
  increment() => _pageIndex;
}

And I also try to call _pageIndex and change its value like this:

onPressed: () {
  final getPageIndex _getPageIndex = Get.put(getPageIndex());
  _getPageIndex._pageIndex = 2;
},

But I am getting error:

enter image description here

The setter '_pageIndex' isn't defined for the type 'getPageIndex'.
Try importing the library that defines '_pageIndex', correcting the name to the name of an existing setter, or defining a setter or field named '_pageIndex'.

What is the problem? How can I solve it?

CodePudding user response:

The problem is underscoring, which makes your variable private. Just remove it.

  int pageIndex = 0;
  • Related