Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter | How can I use the data in void initState?

Hi, I want to use the pages variable in initState inside the BODY tag.

class LoggedInPage extends StatefulWidget {
  final GoogleSignInAccount user;

  const LoggedInPage({Key? key, required this.user}) : super(key: key);

  @override
  _LoggedInPageState createState() => _LoggedInPageState();
}

class _LoggedInPageState extends State<LoggedInPage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;
  int _currentPosition = 0;


  @override
  void initState() {
    _tabController = TabController(length: 2, vsync: this);
    super.initState();
    final _pages = [
      BT(),
      UZEMPage(user: widget.user),
    ];
  }

But they do not see each other.

body: _pages[_currentPosition],

Error description

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Undefined name ‘_pages’.
Try correcting the name to one that is defined, or defining the name.

Thank you in advance for your support.

>Solution :

_pages is a local variable in the method. you have to declare it in the class (under or above _currentPosition).

class LoggedInPage extends StatefulWidget {
  final GoogleSignInAccount user;

  const LoggedInPage({Key? key, required this.user}) : super(key: key);

  @override
  _LoggedInPageState createState() => _LoggedInPageState();
}

class _LoggedInPageState extends State<LoggedInPage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;
  int _currentPosition = 0;
  late List _pages;

  @override
  void initState() {
    _tabController = TabController(length: 2, vsync: this);
    super.initState();
   _pages = [
      BT(),
      UZEMPage(user: widget.user),
    ];
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading