实例成员'userEmail'在初始化器中无法访问

问题描述

以上错误消息正在我的代码中显示。请看下面:

class BottomNaviBar extends StatefulWidget {
  static const String id = 'bottom_navi_bar';

  @override
  _BottomNaviBarState createState() => _BottomNaviBarState();
}

class _BottomNaviBarState extends State<BottomNaviBar> {
  int selectedIndex = 0;
  bool showRecipeNotificationBadge = false;
  bool showProfileNotificationBadge = false;
  final _auth = FirebaseAuth.instance;
  FirebaseUser loggedInUser;
  String userEmail;

  @override
  void initState() {
    super.initState();
    getCurrentUser();
  }

  void getCurrentUser() async {
    try {
      final user = await _auth.currentUser();
      if (user != null) {
        loggedInUser = user;
          userEmail = user.email;
      }
    } catch (e) {
      print(e);
    }
  }


  List<Widget> _widgetOptions = <Widget>[
    RecipeBlog(),FavouritesScreen(userEmail: userEmail,),//this is what is causing the error
    ProperHomeScreen(),ProfileScreen(),];

  ...

这很奇怪,因为如果我将实际用户的电子邮件地址作为硬编码字符串传递到FavouritesScreen()中,则代码可以正常工作。但这不能正常工作。

有什么想法吗?

于20/10/20更新:

有关更多上下文,这是build的代码:

  @override
  Widget build(BuildContext context) {

    List<Widget> _widgetOptions() {
      return [
        RecipeBlog(),ProperHomeScreen(),];
    }

    return Scaffold(
      body: Center(
        child: _widgetOptions.elementAt(selectedIndex),bottomNavigationBar: Theme(
        data: Theme.of(context).copyWith(
          // sets the background color of the `BottomNavigationBar`
          canvasColor: Color(0xFF150A42),// sets the active color of the `BottomNavigationBar` if `Brightness` is light
        ),child: BottomNavigationBar(
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(Icons.search),title: Container(),BottomNavigationBarItem(
              icon: Icon(Icons.favorite,color: Colors.redAccent,BottomNavigationBarItem(
              icon: Stack(children: <Widget>[
                Icon(Icons.forum),showRecipeNotificationBadge != false
                    ? Positioned(
                  top: 0.0,right: 0.0,child: Icon(Icons.brightness_1,size: 12.0,color: Color(0x00000000)),)
                    : Positioned(
                  top: 0.0,color: Colors.redAccent),]),BottomNavigationBarItem(
              icon: Stack(children: <Widget>[
                Icon(Icons.person),showProfileNotificationBadge != false
                    ? Positioned(
                  top: 0.0,],currentIndex: selectedIndex,selectedItemColor: Color(0xFF150A42),backgroundColor: Colors.white,unselectedItemColor: Colors.black38,onTap: _onItemTapped,type: BottomNavigationBarType.fixed,floatingActionButton: selectedIndex == 0 ? null : FloatingActionButton(
        backgroundColor: Color(0xff02aab0),onPressed: () {Navigator.push(
          context,MaterialPageRoute(builder: (context) => BottomNaviBar()),);},child: Icon(Icons.search),);
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)