简体   繁体   English

如何在不重建的情况下渲染 Firebase 中的图像?

[英]How to render image from Firebase without rebuilding?

I'm trying to implement profile image picking in my flutter app using Firebase Storage.我正在尝试使用 Firebase 存储在我的 flutter 应用程序中实现配置文件图像选择。 I use image_picker to get the image and upload it to Firebase, get the download link and add the download link to the imgsrc field in the cloud firestore, from where I can render the NetworkImage.我使用 image_picker 获取图像并将其上传到 Firebase,获取下载链接并将下载链接添加到云 firestore 中的imgsrc字段,从那里我可以渲染 NetworkImage。

Center(
          child: Stack(
            children: [
              buildImage(),
              Positioned(
                bottom: 5,
                right: 5,
                child: GestureDetector(
                    onTap: showPhotoAlertDialog,
                    child: buildEditIcon(Color(0xff407bff))),
              ),
            ],
          ),
        ),

How can I get the default Icons.person kind image for when the user has no profile image, and get the image from the database otherwise?当用户没有个人资料图像时,如何获取默认的Icons.person类型图像,否则如何从数据库中获取图像? The code I'm using right now is as follows:我现在使用的代码如下:

Widget buildImage() {
    return CircleAvatar(
      backgroundImage: NetworkImage(loggedInUser.imgsrc ??
          'https://th.bing.com/th/id/R.945f33b643f2ceffcdae90fb57c61854?rik=XcI0SYBgSefoCA&riu=http%3a%2f%2fgetdrawings.com%2ffree-icon-bw%2fanonymous-avatar-icon-19.png&ehk=5n%2buJG66CeLQZsmhaMt8gag5rXuM3TdebAL6W35K1E4%3d&risl=&pid=ImgRaw&r=0'),
      backgroundColor: Colors.grey[350],
      radius: 100,
    );
  }

I created an Alert Dialog widget to choose whether to choose the image from camera or from the gallery.我创建了一个警报对话框小部件来选择是从相机还是从图库中选择图像。

showPhotoAlertDialog() {
    AlertDialog alert = AlertDialog(
      title: Text("Upload from"),
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          TextButton(
            onPressed: () {
              imageFromCamera()
                  .then((value) => uploadFile())
                  .whenComplete(() => postSource());
              setState(() {});                               ----->
            },
            child: Text("Upload from camera"),
          ),
          TextButton(
            onPressed: () {
              imageFromGallery().then((value) => uploadFile());
              postSource();
              setState(() {});
            },
            child: Text("Upload from gallery"),
          )
        ],
      ),
    );

    showDialog(
      context: context,
      builder: (BuildContext context) {
        return alert;
      },
    );
  }

To upload the image to storage and post the source to cloud firestore, I use the following methods:要将图像上传到存储并将源发布到云 firestore,我使用以下方法:

Future uploadFile() async {
    if (file == null) return;

    final fileName = path.basename(file!.path);
    final destination = 'files/$fileName';

    task = FirebaseApi.uploadFile(destination, file!);
    setState(() {});

    if (task == null) return;

    final snapshot = await task!.whenComplete(() {});
    urlDownload = await snapshot.ref.getDownloadURL();

    print('Download-Link: $urlDownload');
  }

  postSource() async {
    FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance;

    await firebaseFirestore
        .collection("users")
        .doc(user?.uid)
        .update({'imgsrc': urlDownload});
  }

The link gets uploaded properly and I'm able to get the link in my NetworkImage , but it doesn't get rendered immediately.链接已正确上传,我可以在我的NetworkImage中获取链接,但不会立即呈现。 I have to close the parent drawer and open it again to get it.我必须关闭父抽屉并再次打开它才能得到它。 I call setState(){} as well after posting the source, but it doesn't make any difference.我在发布源代码后也调用了setState(){} ,但这没有任何区别。 How can I get the image without having to close and open the drawer?如何在不必关闭和打开抽屉的情况下获取图像?

Any help would be appreciated!任何帮助,将不胜感激! Thanks谢谢

You also have to update image in model class or in this imgsrc also just add this line above setState in onPressed of TextButton.您还必须在 model class 或此 imgsrc 中更新图像,也只需在 TextButton 的 onPressed 中的 setState 上方添加此行。

loggedInUser.imgsrc = urlDownload;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 .map 方法渲染来自 Firebase 存储的图像? - How do you render an image from Firebase Storage using .map method? 如何在没有 EventListener 的情况下从 Firebase 检索数据? - How to retrieve data from Firebase without EventListener? 如何从 Firebase 存储中获取图像文件名? - How to get image filename from Firebase Storage? 如何从firebase下载用户图片到UI - How to download user image from firebase to UI 如何显示来自 firebase 的图像? - how can i display image from firebase? 如何渲染依赖 firebase 异步函数的组件? - How to render components that rely on firebase async functions? 如何在 flutter 不刷新的情况下从 firebase 检索数据 - How can I retrieve data from firebase without refresh in flutter Android:如何修复从 Firebase 拍摄的模糊的 Google 个人资料图片? - Android: How To Fix Blurry Google Profile Image Taken From Firebase? 如何从 Flutter 中的 Firebase 存储中检索图像 URL? - How to retreive a image URL from Firebase Storage in Flutter? 如果没有 function 代码更改,如何使用 Codepipeline 部署 AWS Lambda 而无需重建它? - How to deploy AWS Lambda using Codepipeline without rebuilding it if there are no function code changes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM