【问题标题】:Error showing when document is deleted from firestore with streambuilder (flutter)使用streambuilder(flutter)从firestore中删除文档时显示错误
【发布时间】:2021-05-23 13:58:48
【问题描述】:

大家好,如果我的英语不清楚,首先我很抱歉,我正在做一个个人项目。因此,我在 Firestore 文档上使用 StreamBuilder,其用户 ID 来自“用户”集合。因此,我已检索“imageUrl”字段并将其显示在我的应用程序的图像网络中,因此,我有“删除帐户”按钮,此按钮将从 firebase auth 中删除该帐户,并删除 streambuilder 侦听的文档.

因此,发生错误是因为流构建器将构建 ImageNetwork 并从文档字段中检索 URL。 有什么办法可以处理这个错误吗?

这是将返回 NetworkImage 的 streamBuilder 的代码

StreamBuilder<DocumentSnapshot>(
                    stream: Firestore.instance
                        .collection('Users')
                        .document(user.getID())
                        .snapshots(),
                    builder:
                        (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
                      print(snapshot.connectionState);
                      var userDocument = snapshot.data;
                      if (userDocument.data.length == 0) {
                        return const Center(
                          child: Text(
                            "Not Available",
                            style:
                                TextStyle(fontSize: 30.0, color: Colors.grey),
                          ),
                        );
                      } else
                        return AvatarGlow(
                          glowColor: Colors.redAccent,
                          endRadius: 90,
                          child: Material(
                            elevation: 8.0,
                            shape: CircleBorder(),
                            child: CircleAvatar(
                              backgroundColor: Colors.grey[100],
                              child: ClipOval(
                                child: FadeInImage(
                                  image: NetworkImage(
                                      userDocument['imageUrl'] ??
                                          'https://picsum.photos/250?image=9'),
                                  placeholder: AssetImage('assets/noImage.png'),
                                ),
                              ),
                              radius: 70,
                            ),
                          ),
                        );
                    },
                  ),

调试错误

The getter 'length' was called on null.
Receiver: null
Tried calling: length
The relevant error-causing widget was
StreamBuilder<DocumentSnapshot>

如果 else 阻塞,解决方案就开启

StreamBuilder<DocumentSnapshot>(
                stream: Firestore.instance
                    .collection('Users')
                    .document(user.getID())
                    .snapshots(),
                builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
                  if (snapshot.data != null && snapshot.data.exists) {
                    var userDocument = snapshot.data;
                     // return something
                    } 
                  }

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    对于不存在的文档userDocument.data 将返回null,因此userDocument.data.length 会抛出您得到的错误。

    我猜你想检查文档是否存在,你会这样做:

    if (userDocument.exists) {
    

    另请参阅DocumentSnapshot class 上的参考文档,这是您的userDocument 的对象类型。

    【讨论】:

    • 我试过了,它说 ``` The getter 'data' was called on null。接收者:空尝试调用:数据```
    • 这与您之前显示的错误不同。 1) 将var userDocument = snapshot.data; 调用移动到else 块中。 2) 将条件改为if (snapshot.data != null &amp;&amp; snapshot.data.exists) {
    【解决方案2】:

    好的。那么,这些StreamBuilder(s) 和FutureBuilder(s) 应该如何使用如下: 注意:以下代码应该在您的 Builder 函数中。

    if(snapshot.hasData){
      // Your normal functioning of the app will follow here.
      // Now that you know that your snapshot contains data,
      // then you access the data.
      var userDocument = snapshot.data;
      // Now, you can check if the userDocument exists or not.
    }
    
    else if(snapshot.hasError){
      // return an error message or do something else.
    }
    // return any default Widget while the data is being loaded.
    return CircularProgressIndicator();
    

    另外,我建议一旦用户请求删除他/她的帐户,您应该导航回主屏幕...

    【讨论】:

    • 谢谢,我已经解决了,当用户删除账号时,会自动跳转到登录界面。
    猜你喜欢
    • 1970-01-01
    • 2021-02-26
    • 2020-10-31
    • 2020-03-04
    • 2023-01-01
    • 2020-09-01
    • 2020-07-30
    • 1970-01-01
    • 2021-03-16
    相关资源
    最近更新 更多