无法在初始化程序中访问实例成员“小部件”。?

时间:2021-06-13 06:51:26

标签: flutter dart

我在 StatefulWidget 小部件中有一个 UserType 变量,我想要做的是当用户在不同的 BottomNavigationBarItem 之间导航时将此 UserType 变量的值传递到不同的 Screen,我想将此 UserType 变量传递到上传页面() 像下面的代码一样,但我遇到了错误,无法在初始化程序中访问实例成员“小部件”。请帮帮我?

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]

}
}

1 个答案:

答案 0 :(得分:-1)

更改您的代码如下,

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]

}
}