0

I have a UserType variable inside StatefulWidget widget, and what I am trying to do is to pass the value of this UserType variable into different Screen when The user Navigates between different BottomNavigationBarItem, I want to pass this UserType variable in to Upload page() Like below code, but I face the Error ,The instance member 'widget' can't be accessed in an initializer. Please help me?

the Value of UserType variable is alredy passed from othre page using HomePagecontroller(UserType:controllUserType)

User curentuser= _firebaseAuth.currentUser;
class HomePage extends StatefulWidget {
  final String UserType;
  const HomePage({Key key,@required this.UserType}):super(key: key);
  @override
  _HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
  final List<Widget>_children =
  [
      TimeLinePage(),
      SearchPage(), //search(),
      UploadPage(UserSID: curentuser.uid, usertypes: widget.UserType),
      NotificationsPage(),
      ProfilePage(userProfileID: curentuser.uid),
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: buildHomeScreen(),
    );
  }
  Scaffold buildHomeScreen() {
    return Scaffold(
      backgroundColor: Colors.black,
      body: _children[_CurrentIndex],
      bottomNavigationBar: CupertinoTabBar(
        currentIndex: _CurrentIndex,
        backgroundColor: Colors.black,
        onTap: onTabchangePage,
        activeColor: Colors.green,
        inactiveColor: Colors.white,
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('home'),),
          BottomNavigationBarItem(icon: Icon(Icons.search)),
          BottomNavigationBarItem(icon: Icon(Icons.photo_camera, size: 40,)),
          BottomNavigationBarItem(icon: Icon(Icons.notifications)),
          BottomNavigationBarItem(icon: Icon(Icons.person_add_alt_1_sharp)),
        ],
      ),
    );
  }
}
void onTabchangePage(int index) {
  setState(() {
    _CurrentIndex=  index;
  });[here is the image for the error][1]

}
}
beby
  • 113
  • 9
  • See https://stackoverflow.com/a/67643624/. If `_children` depends on other members, instead initialize in a constructor body, in `initState`, or mark it `late`. – jamesdlin Jun 13 '21 at 06:57

1 Answers1

-1

Change your code as follows,

User curentuser= _firebaseAuth.currentUser;
class HomePage extends StatefulWidget {
  final String UserType;
  late List<Widget>_children;
  HomePage({Key key, required this.UserType}):super(key: key){
  _children = 
  [
      TimeLinePage(),
      SearchPage(), //search(),
      UploadPage(UserSID: curentuser.uid, usertypes: widget.UserType),
      NotificationsPage(),
      ProfilePage(userProfileID: curentuser.uid),
  ];
}
  @override
  _HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: buildHomeScreen(),
    );
  }
  Scaffold buildHomeScreen() {
    return Scaffold(
      backgroundColor: Colors.black,
      body: widget._children[_CurrentIndex],
      bottomNavigationBar: CupertinoTabBar(
        currentIndex: _CurrentIndex,
        backgroundColor: Colors.black,
        onTap: onTabchangePage,
        activeColor: Colors.green,
        inactiveColor: Colors.white,
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('home'),),
          BottomNavigationBarItem(icon: Icon(Icons.search)),
          BottomNavigationBarItem(icon: Icon(Icons.photo_camera, size: 40,)),
          BottomNavigationBarItem(icon: Icon(Icons.notifications)),
          BottomNavigationBarItem(icon: Icon(Icons.person_add_alt_1_sharp)),
        ],
      ),
    );
  }
}
void onTabchangePage(int index) {
  setState(() {
    _CurrentIndex=  index;
  });[here is the image for the error][1]

}
}
Lakmal Fernando
  • 1,420
  • 6
  • 14