When using StreamBuilder it shows below error.
The following _CastError was thrown building StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(dirty, state: _StreamBuilderBaseState<DocumentSnapshot<Map<String, dynamic>>, AsyncSnapshot<DocumentSnapshot<Map<String, dynamic>>>>#65942):
Null check operator used on a null value
My try:
I set CircularProgressIndicator for ConnectionState.none
,
ConnectionState.waiting
,!streamSnapshot.hasData
It didn’t work.
There is my StreamBuilder,
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection("users")
.doc(FirebaseAuth.instance.currentUser!.uid)
.snapshots(),
builder: (context, streamSnapshot) {
var userData = streamSnapshot.data!;
if (streamSnapshot.connectionState == ConnectionState.none ||
streamSnapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(color: darkblueColor),
);
}
if (!streamSnapshot.hasData) {
return const Center(
child: CircularProgressIndicator(color: darkblueColor),
);
}
return ListView(
padding: EdgeInsets.zero,
children: [
UserAccountsDrawerHeader(
accountName: Text(userData['applicationStatus'] == "none"
? 'Display Name'
: userData['displayName']),
accountEmail: Text(userData['applicationStatus'] == "none"
? "Your profile not yet published."
: userData['phoneNumber']),
currentAccountPicture: CircleAvatar(
backgroundColor: whiteColor,
child: ClipOval(
child: Image.network(
userData['applicationStatus'] == "none"
? '..........'
: userData['imageUrl'],
fit: BoxFit.cover,
width: 90,
height: 90,
),
),
),
decoration: BoxDecoration(
color: appBarColor,),
),
ListTile(
leading: Icon(Icons.account_circle),
title: Text('Profile'),
onTap: () => null,
),
],
);
});
2
Answers
Before the state is having data you are trying to access it. Remove the same to solve your error
Use like this,