不能将 'Null' 类型的值分配给 const 构造函数中类型为"String"的参数。尝试使用子类型,或删除关键字"const"


const homeScreenItems = [
FeedScreen(),
SearchScreen(),
AddPostScreen(),
Text('notification'),
ProfileScreen(uid: FirebaseAuth.instance.currentUser!.uid),
];

上面的代码是我的全局变量页面。我想将存储在Firestore中的uid传递给ProfileScreen((,但我收到了上面标题的错误,即在const构造函数中,不能将"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,
),
),
),
],
);
}
}

这里的问题是您在homeScreenItems中使用const-这会在编译时设置变量常量,并且不能更改。它通常用于固定值,如具有固定字符串值的Text小部件。

此外,使用bang(!(运算符并不能保证该值不会为null。这只会确保编译器的值不为null。

根据您的用例,您可以从homeScreenItems中删除const,并在用户登录并能够获取FirebaseAuth.instance.currentUser后添加ProfileScreen()

List homeScreenItems = [
FeedScreen(),
SearchScreen(),
AddPostScreen(),
Text('notification'),  
];
...
if(FirebaseAuth.instance.currentUser != null){
homeScreenItems.add(ProfileScreen(uid: FirebaseAuth.instance.currentUser!.uid));
}

像这个一样使用List<Widget>而不是const

List<Widget> homeScreenItems = [...];

相关内容

最新更新