我在访问_MenuScreenState上的变量user时遇到了问题:
class MenuScreen extends StatefulWidget {
final User user;
MenuScreen(this.user);
@override
_MenuScreenState createState() => _MenuScreenState();
}
class _MenuScreenState extends State<MenuScreen> {
final User userInMenu = widget.user;
}
显示的问题是"无法在初始化器中访问实例成员'widget'。尝试用不同的表达式"替换对实例成员的引用。
您必须在initState
中这样做:
class MenuScreen extends StatefulWidget {
final User user;
MenuScreen(this.user);
@override
_MenuScreenState createState() => _MenuScreenState();
}
class _MenuScreenState extends State<MenuScreen> {
User userInMenu = widget.user;
@override
void initState() {
super.initState();
userInMenu = widget.user;
}