Home > Back-end >  unexpected null error coming in flutter web on stream builder ,working fine on android
unexpected null error coming in flutter web on stream builder ,working fine on android

Time:01-01

I have the below code in my profile page. The code is working absolutely fine in android ,but on web when I open my profile page it is showing

unexpected null value error ,

The code which is pointing to error in debug console :

return Container(
  
  child: StreamBuilder<DocumentSnapshot>(
    stream: FirebaseFirestore.instance
        .collection('users')
        .doc(profileId)
        .snapshots(),
    builder: (context,snapshot) {
      if (!snapshot.hasData) {
        return Container(
            alignment: FractionalOffset.center,
            child: CircularProgressIndicator());
      }

      User user = User.fromDocument(snapshot.data!);

      return Scaffold(
        //all body widgets
      );

the debug console shows

The following TypeErrorImpl was thrown building StreamBuilder<DocumentSnapshot<Object?>>(dirty, state: _StreamBuilderBaseState<DocumentSnapshot<Object?>, AsyncSnapshot<DocumentSnapshot<Object?>>>#556d2):
Unexpected null value.

The relevant error-causing widget was
StreamBuilder<DocumentSnapshot<Object?>>
lib/profile_page.dart:615

CodePudding user response:

I think you should put this line User user = User.fromDocument(snapshot.data!); into else, like this:

if (!snapshot.hasData) { // if snapshot has no data this is going to run
  return Container(
    alignment: FractionalOffset.center,
    child: CircularProgressIndicator());
}
else{ // if snapshot has data this is going to run
  User user = User.fromDocument(snapshot.data!);
}

CodePudding user response:

Check Firebase permissions for the web

  • Related