Home > OS >  Flutter - Null check operator used on a null value Error when use StreamBuilder
Flutter - Null check operator used on a null value Error when use StreamBuilder

Time:01-16

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,
          ),
        ],
      );
    });

CodePudding user response:

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,
      ),
    ],
  );
}

CodePudding user response:

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

builder: (context, streamSnapshot) {

           
  • Related