Home > Software design >  Flutter | How can I use the data in void initState?
Flutter | How can I use the data in void initState?

Time:01-08

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

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

Thank you in advance for your support.

CodePudding user response:

_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),
    ];
  }
  •  Tags:  
  • Related