Home > Back-end >  Is that possible to use the passing data form another page in Statefulwidget without using "wid
Is that possible to use the passing data form another page in Statefulwidget without using "wid

Time:09-28

Hi guy I kind of new to this tool and I'd like some of your help. Let's me explain what tying to do, I'm have a Nav() Statefulwidget that passing bool form a another page to check that now the person that using the app is "User" or "Guest" and in Nav() I use BottomNavigationBarItem widget. Now the issue is BottomNavigationBarItem need to create the list outside the BuildContext and in the list is contain the page that need to navigate. I have one page that name Profile() that also need to passing the bool to check the user status too and I'd like to use the bool user that already passed to pass the data to Profile() page but it look like I can't call the user outside BuildContext. I even use widget.user but it still got an error said

"The instance member 'widget' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression"

Is that anyway out that I can call the user outside the BuildContext?

So if there is anyone who can help me I will be very thankful.

Here is the code:

class Nav extends StatefulWidget {
  const Nav({Key? key, required this.user}) : super(key: key);

  final bool user;

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

class _NavState extends State<Nav> {
  int _selectedIndex = 0;
  final bool _user = widget.user;
  final List<Widget> _widgetOptions = <Widget>[
    Home(),
    News(),
    Timeline(),
    Manual(),
    Profile(user: _user),
  ];

  void _onItemTap(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: IndexedStack(
          index: _selectedIndex,
          children: _widgetOptions,
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        backgroundColor: const Color(0xFF20348F),
        selectedItemColor: Colors.amber,
        unselectedItemColor: Colors.white54,
        items: [
          bottomNav(const Icon(Icons.home), 'Home'),
          bottomNav(const Icon(Icons.line_style_outlined), 'News'),
          bottomNav(const Icon(Icons.location_pin), 'Timeline'),
          bottomNav(const Icon(Icons.my_library_books_outlined), 'Manual'),
          bottomNav(const Icon(Icons.person), 'Profile'),
        ],
        currentIndex: _selectedIndex,
        onTap: _onItemTap,
        selectedFontSize: 15.0,
      ),
    );
  }
}

BottomNavigationBarItem bottomNav(Icon Icon, String Txt) {
  return BottomNavigationBarItem(icon: Icon, label: Txt);
}

CodePudding user response:

If you are using null-safety version, you can initialize them like that.

class _NavState extends State<Nav> {
  int _selectedIndex = 0;
  late bool _user;
  late List<Widget> _widgetOptions;

  @override
  void initState() {
    _user = widget.user;
    _widgetOptions = <Widget>[
      Home(),
      News(),
      Timeline(),
      Manual(),
      Profile(user: _user),
    ];
    super.initState();
  }

Also I strongly suggest you to use enum instead of bool for specifying user type.

  • Related