我如何解决此错误无法在初始化程序中访问实例“小部件”

时间:2021-06-15 16:13:06

标签: flutter dart

我正在尝试访问变量 String UserType 和 String userID 的值;在 _HomePageState 上, 我想将 UserType 和 userID 变量的值传递给 ProfilePage() 和 UploadPage(),

IMAGES for my Error

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

这是我的 StatefulWidget 类里面我声明了 Usertype 和 userID 变量,我 想将它们传递给 ProfilePage() 和 UploadPage(),在 _HomePageState 中,

class HomePage extends StatefulWidget {
      String UserType;
      String userID;
     HomePage({Key key,@required this.UserType,@required this.userID}):super(key: key);
      @override
      _HomePageState createState() => _HomePageState();
    }

class _HomePageState extends State<HomePage> {
  int _CurrentIndex=0;
  String owneruerID= widget.userID;
  dynamic uploadusertypes=widget.UserType;

final List<Widget>_children=[
        TimeLinePage(),
        SearchPage(), //search(),
        UploadPage(UserSID:owneruerID,uploadusertypes:uploadusertypes),
        NotificationsPage(),
        ProfilePage(userProfileID:widget.userID),
        ];

 @override
  void initState(){
  super.initState();
    uploadusertypes= widget.UserType;
    owneruerID= widget.userID; 
  }
  @override
  Widget build(BuildContext context) {
return Scaffold(
   body: WillPopScope(
     onWillPop: onwillpops,
     child: 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;
    });

  }
}

images for my Error code

i Correct the Bove but still the Error Exists

Images for The ERROR

class HomePage extends StatefulWidget {
   String UserType;
   String userID;
  HomePage({Key key,@required this.UserType,@required this.userID}):super(key: key);
  @override
  _HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
  String owneruerID;
  dynamic uploadusertypes;

 final List<Widget>_children=[
        UploadPage(UserSID:owneruerID,uploadusertypes:uploadusertypes),
        ProfilePage(userProfileID:owneruerID),
        ];


  void initState(){
    super.initState();
    uploadusertypes= widget.UserType;
    owneruerID = widget.userID;
  }

}

1 个答案:

答案 0 :(得分:0)

String owneruerID;
dynamic uploadusertypes;
List<Widget>_children;

代替

String owneruerID= widget.userID;
  dynamic uploadusertypes=widget.UserType;

// remove and keep just final List<Widget>_children
    final List<Widget>_children=[
            TimeLinePage(),
            SearchPage(), //search(),
            UploadPage(UserSID:owneruerID,uploadusertypes:uploadusertypes),
            NotificationsPage(),
            ProfilePage(userProfileID:widget.userID),
            ];

在初始化状态

@override
  void initState(){
  super.initState();
    uploadusertypes= widget.UserType;
     owneruerID = widget.userID;

//add it here
      _children=[
        TimeLinePage(),
        SearchPage(), //search(),
        UploadPage(UserSID:owneruerID,uploadusertypes:uploadusertypes),
        NotificationsPage(),
        ProfilePage(userProfileID:widget.userID),
        ];