【问题标题】:How can I displaying user image?如何显示用户图像?
【发布时间】:2021-06-14 20:22:40
【问题描述】:

我显示用户个人资料图片,我有 3 个可以显示的选项。第一个是当前拾取的图像,它是从相机或库中拾取的,如果有图像,它将被显示。然后用户可以保护它,以便在用户重新启动应用程序时也会显示它,在这种情况下我会显示保存的图像。然后是我的第三个选项,如果没有保存图片并且目前没有选择图片,那么我将显示默认图像,这在我的资产中。这是我的问题,我该怎么做,因为这个默认图片也会显示,如果用户删除当前保存的图片,所以如果他改回“没有拍照”,那么默认图片将再次显示。但是如何在我的代码中实现这一点?图片作为文件保存在存储中,并且作为 url 保存在 Firestore 中 这是我的 Circle 头像,用于显示保存图像以及保存前选择的图像。

   Container(
                                      child: Center(
                                              child:
                                            CircleAvatar(
                                              radius: 70,
                                              
                                              foregroundImage: _pickedImage!=null? FileImage(_pickedImage): NetworkImage ( userData.url),
                                            ),
                              ),
                                          decoration: new BoxDecoration(
                                            shape: BoxShape.circle,

这是我的方法:

   onPressed: () async {
                if (_formKey.currentState.validate()) {
                  final ref= FirebaseStorage.instance.ref().child('user_profile_pictures').child(user.uid+'.jpg');
                  await ref.putFile(_pickedImage);
                  final url = await ref.getDownloadURL();
                  _curenturl= url;

                  String authError =
                      await DatbaseService(uid: user.uid).updateUserData(
                    _currentusername ?? userData.user,
                    _currentfullname ?? userData.fullname,
                    _currentemail ?? userData.email,
                    _currentpassword ?? userData.password,
                          _curenturl ??userData.url,

                  );
                  if (authError != null) {

这是我将当前 url 设置为 null 以及 userData.url 的按下方法

      onTap: () {

                 DatbaseService(uid: user.uid).updateUserData(
                   _currentusername ?? userData.user,
                   _currentfullname ?? userData.fullname,
                   _currentemail ?? userData.email,
                   _currentpassword ?? userData.password,
                   _curenturl =null,
            );
                Navigator.pop(context);
              },

所以你可以看到我再次将 url 设置为 null。即时通讯这样做是为了不再有保存的图片。然后我想显示我的资产中的默认图像。我试图用那张图片更新我当前的 url,但 url 是一个字符串,我的图片是一个资产图像。

【问题讨论】:

    标签: flutter google-cloud-platform google-cloud-firestore user-input


    【解决方案1】:

    你可以试试我的代码:

      ClipRRect(
                borderRadius: BorderRadius.circular(60),
                child: Container(
                  height: 100,
                  width: 100,
                  decoration: BoxDecoration(
                    color: Colors.grey.shade300,
                  ),
                  child: image != null
                      ? Image.file(
                          image,
                          fit: BoxFit.cover,
                        )
                      : networkImg != null && networkImg != ''
                          ? Image.network(
                              networkImg,
                              fit: BoxFit.cover,
                            )
                          : Image.asset(
    // Your image path is here when image is no available.
    ),
                ),
              ),
    
    

    请告诉我它是否有效。

    【讨论】:

    • 它像以前一样工作,但是当我添加这一行 AssetImage('assets/profilepictureer.png') 时,它说类型'AssetImage'不是类型'Widget?'的子类型,而不是容器/跨度>
    • @newuser122 我已经编辑了我的代码,所以请立即尝试。您需要使用 Image.asset 属性。
    • 完美,谢谢,但是如何删除存储中的文件?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 2021-09-28
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多