skip to Main Content

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


  1. Before the state is having data you are trying to access it. Remove the same to solve your error

    builder: (context, streamSnapshot) {
    
    👉   var userData = streamSnapshot.data!;        ❌ remove this line
    
      if (streamSnapshot.connectionState == ConnectionState.none ||
         ....
      }
    
    Login or Signup to reply.
  2. Use like this,

    builder: (context, streamSnapshot) {
      
      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(streamSnapshot.data!['applicationStatus'] == "none"
                ? 'Display Name'
                : streamSnapshot.data!['displayName']),
            accountEmail: Text(streamSnapshot.data!['applicationStatus'] == "none"
                ? "Your profile not yet published."
                : streamSnapshot.data!['phoneNumber']),
            currentAccountPicture: CircleAvatar(
              backgroundColor: whiteColor,
              child: ClipOval(
                child: Image.network(
                  userData['applicationStatus'] == "none"
                      ? '..........'
                      : streamSnapshot.data!['imageUrl'],
                  fit: BoxFit.cover,
                  width: 90,
                  height: 90,
                ),
              ),
            ),
            decoration: BoxDecoration(
              color: appBarColor,),
          ),
          ListTile(
            leading: Icon(Icons.account_circle),
            title: Text('Profile'),
            onTap: () => null,
          ),
        ],
      );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search