firebase 类型为“Null”的值不能分配给常量构造函数中类型为“String”的参数,请尝试使用子类型,或移除关键字“const”

cedebl8k  于 2023-02-16  发布在  其他
关注(0)|答案(1)|浏览(143)
const homeScreenItems = [
  FeedScreen(),
  SearchScreen(),
  AddPostScreen(),
  Text('notification'),
  ProfileScreen(uid: FirebaseAuth.instance.currentUser!.uid),
];

上面的代码是我的全局变量页。我想将存储在Firestore中的uid传递给ProfileScreen(),但我收到了上面标题的错误,即类型为“Null”的值无法分配给常量构造函数中类型为“String”的参数。请尝试使用子类型,或删除关键字“const”。
我已经在ProfileScreen()中将uid声明为final,但仍然收到上述错误
//这里是ProfileScreen()的代码

class ProfileScreen extends StatefulWidget {
  final String uid;
  const ProfileScreen({Key? key,required this.uid}) : super(key: key);

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

class _ProfileScreenState extends State<ProfileScreen> {
  var userData = {};
  @override
  void initState() {
    getUserData();
    super.initState();
  }

  getUserData() async {
    try {
      var snap = await FirebaseFirestore.instance
          .collection('users')
          .doc(widget.uid)
          .get();
      setState(() {
        userData = snap.data()!;
      });
    } catch (e) {
      showSnakBar(e.toString(), context);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: mobileBackgroundColor,
        title: Text(userData['name']),
        centerTitle: false,
      ),
      body: ListView(
        children: [
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              children: [
                Row(
                  children: [
                    CircleAvatar(
                      radius: 40,
                      backgroundImage: NetworkImage(
                        'https://images.unsplash.com/photo-1647185255712-b5b8687b2a25?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1722&q=80',
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: Column(
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            mainAxisSize: MainAxisSize.max,
                            children: [
                              buildStatColumn(20, 'posts'),
                              buildStatColumn(150, 'followers'),
                              buildStatColumn(10, 'following'),
                            ],
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: [
                              FollowButton(
                                backgroundColor: mobileBackgroundColor,
                                borderColor: Colors.grey,
                                text: 'Edit Profile',
                                textColor: primaryColor,
                                function: () {},
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
                Container(
                  alignment: Alignment.centerLeft,
                  child: Padding(
                    padding: const EdgeInsets.only(top: 15),
                    child: Text(
                      'username',
                      style: TextStyle(fontWeight: FontWeight.bold),
                    ),
                  ),
                ),
                Container(
                  alignment: Alignment.centerLeft,
                  child: Padding(
                    padding: const EdgeInsets.only(top: 1),
                    child: Text('some bio'),
                  ),
                ),
              ],
            ),
          ),
          const Divider(),
        ],
      ),
    );
  }

  Column buildStatColumn(int num, String lable) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          num.toString(),
          style: const TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.bold,
          ),
        ),
        Container(
          margin: const EdgeInsets.only(top: 4),
          child: Text(
            lable,
            style: const TextStyle(
              fontSize: 15,
              fontWeight: FontWeight.w400,
              color: Colors.grey,
            ),
          ),
        ),
      ],
    );
  }
}
lf5gs5x2

lf5gs5x21#

这里的问题是你在homeScreenItems中使用的是const--这会在编译时设置变量常量,并且不能更改。它通常用于固定值,比如具有固定字符串值的文本小部件。
此外,使用bang(!)操作符并不能保证值不为空,只能向编译器保证值不为空。
根据您的用例,您可以从homeScreenItems中删除const,并在用户登录后添加ProfileScreen(),这样您就可以获取FirebaseAuth.instance.currentUser

List homeScreenItems = [
  FeedScreen(),
  SearchScreen(),
  AddPostScreen(),
  Text('notification'),  
];

...

if(FirebaseAuth.instance.currentUser != null){
  homeScreenItems.add(ProfileScreen(uid: FirebaseAuth.instance.currentUser!.uid));
}

相关问题