【问题标题】:Not Showing Placeholder Image even when url in networkimage become's null即使网络图像中的 url 变为空,也不显示占位符图像
【发布时间】:2021-06-09 17:26:32
【问题描述】:

GetPhotoUrlStream 提供存储在我的 Cloud Firebase FireStore 中的个人资料照片 (data['profilePhoto']) 的 Url 流。然后被networkimage用来显示个人资料照片(圆形头像)

 class GetUserPhotoUrlStream extends StatelessWidget {
      final String documentId; // This is your User UID
    
      GetUserPhotoUrlStream(this.documentId);
    
      @override
      Widget build(BuildContext context) {
        DocumentReference users = FirebaseFirestore.instance.collection('users').doc(documentId);
        return StreamBuilder<DocumentSnapshot>(
          stream: users.snapshots(),
          builder:  (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
    
            if (snapshot.hasError) {
              return Image.asset('assets/images/NouserImage.png');
            }
    
            if (snapshot.connectionState == ConnectionState.waiting) {
              return CircularProgressIndicator();
            }
    
            Map<String, dynamic> data = snapshot.data.data();
            return  CircleAvatar(
                maxRadius: 80,
                backgroundColor: Colors.grey,
                child: ClipOval(child: FadeInImage(placeholder: AssetImage('assets/images/NouserImage.png'),image: NetworkImage("${data['profilePhoto']}"),),),
               
            );
          },
        );
      }
    }

removeUserPhotoUrl 将 GetUserPhotoUrlStream 正在使用的“profilePhoto”更新为 null。

  Future<void> removeUserPhotoUrl(BuildContext context) async
  {
    var user = _auth.currentUser;
    DocumentReference users = FirebaseFirestore.instance.collection('users').doc(user.uid);
     users.update({'profilePhoto':null}).then((_){
      Navigator.pop(context);
    });
    await deleteUserImage(context);
    notifyListeners();
  }

当使用 removeUserPhotoUrl 将 data['profilePhoto'] 的值设为 null 时,它应该显示占位符图像,它提供了一个assetImage,而不是它给出了一个错误

错误信息

====================================================================================================

======== Exception caught by image resource service ================================================
Invalid argument(s): No host specified in URI file:///null
====================================================================================================

此外,当应用程序处于 HotReload 或 HotRestart 时,错误消失并开始向我显示 PlaceHolder(资产图像)

请帮助。我想在“profilePhoto”变为空时显示占位符(资产图像)

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore setstate


    【解决方案1】:

    试试这个并确保你的图片放在 pubspec.yaml 中

    if (!snapshot.hasData) {
                      return Image.asset('assets/images/NouserImage.png');
                    }
    

    【讨论】:

    • 感谢您回答问题,但这对我不起作用,并且错误仍然与 Cloud firebase firestore 中的相同,不仅有 profilePhoto 的数据,而且还有其他详细信息,例如不能为空,所以snapshot.hasData 将始终有一些数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 2016-07-20
    相关资源
    最近更新 更多