错误:
无法在初始化程序中访问实例成员“userId”。
尝试用不同的表达式替换对实例成员的引用dartimplicit_this_reference_in_initializer
我面临的问题:
我想在 nav.dart 中将用户 uid(即 userId.uid)初始化为 profileId,以便我可以在个人资料页面中使用 widget.profileId 来显示个人资料页面中的当前用户详细信息。但是我收到了上述错误。
请帮帮我:)
谢谢
这是 nav.dart
.
.
import 'package:justpoll/screens/home_page/home.dart';
import 'package:justpoll/screens/trending.dart';
import 'package:justpoll/screens/profile/profile_page.dart';
class Nav extends StatefulWidget {
@override
_NavState createState() => _NavState();
}
class _NavState extends State<Nav> {
UserModel userId;
int _selectedIndex = 0;
List<Widget> _widgetOptions = <Widget>[
Home(),
Trending(),
CreatePoll(),
// ChatHomeScreen(),
ProfilePage(profileId: userId.uid),
];
.
.
.
这是 ProfilePage.dart .
.
import 'package:justpoll/screens/profile/edit_profile.dart';
import 'package:justpoll/Constants.dart';
class ProfilePage extends StatefulWidget {
final String profileId;
ProfilePage({this.profileId});
@override
_ProfilePageState createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
FirebaseAuth auth = FirebaseAuth.instance;
.
.
最佳答案
发生上述错误是因为您声明了变量 userId
直接在类内部,您试图将非静态变量初始化为 profileId
.因为您正在使用 StatefulWidget
,尝试在您的 initState() 方法中初始化它。
UserModel userId;
int _selectedIndex = 0;
List<Widget> _widgetOptions;
void initState() {
super.initState();
_widgetOptions = <Widget>[
Home(),
Trending(),
CreatePoll(),
// ChatHomeScreen(),
ProfilePage(profileId: userId.uid),
];
}
关于firebase - 错误 "Try replacing the reference to the instance member with a different ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66122135/