首页 > 解决方案 > 我如何修复此错误无法在初始化程序中访问实例“小部件”

问题描述

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

我的错误的图像

    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".

这是我声明 Usertype 和 userID 变量的我的 StatefulWidget 类,我想将它们传递给 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;
    });

  }
}

我的错误代码的图像

i Correct the Bove but still the Error Exists

错误的图像

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;
  }

}

标签: flutterdart

解决方案


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),
        ];

推荐阅读