firebase - 错误 "Try replacing the reference to the instance member with a different "

标签 firebase flutter dart

错误:
无法在初始化程序中访问实例成员“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/

相关文章:

python - 使用 Python 将数据发布到 Firebase

flutter - Flutter中如何使用字符串访问类成员

dart - 如何从构建器外部在 Flutter 上弹出警报对话框

swift - Firebase 存储图像返回空白

使用 LDAP 的 Firebase 身份验证

android - Flutter Driver 测试超时

flutter - Flutter:在SharedPreferences中保存和获取多个值

dart - 如何在 Flutter 中使用其他组件在页面中实现选项卡 Controller ?

使用 Expo 和 Firebase 的 iOS 应用奖励推荐

flutter - 无法在 vs 代码中打开模拟器