flutter - 我如何修复此错误无法在初始化程序中访问实例“小部件”
问题描述
我正在尝试访问变量 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;
}
}
解决方案
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),
];
推荐阅读
- javascript - 有没有办法在新点击时重置突出显示功能和信息面板?
- echarts - 如何实现 eCharts 反向功能,同时保持 y 轴标题在顶部?
- javascript - 尝试使用monogdb expess nodejs上传图片,遇到问题
- android - 如何使用 api > 23 在 android 中以颜色绘制表情符号⚠
- javascript - 如何修复错误:“解析错误:意外的保留字 'await'。(10:22)eslint”?
- html - 使用 .css 文件将内容添加到表格单元格
- python - pycharm 中的 Deezer Spleet
- arrays - 在groovy中以数组的形式显示字段值
- sql - 使用依赖于表字段的子查询中的数据更新表
- node.js - Firebase Cloud Functions 上的 Firestore 查询性能问题