流生成器上的flutter web出现意外的null错误,在android上运行良好



我的配置文件页面中有以下代码。代码在android中运行得非常好,但在网络上,当我打开我的个人资料页面时,它显示

意外的零值错误

调试控制台中指向错误的代码:

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 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

我认为应该将这行User user = User.fromDocument(snapshot.data!);放入else,如下所示:

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!);
}

检查web 的Firebase权限

最新更新