简体   繁体   English

我将图像上传到 firebase 存储,但在 firebase 存储中看不到图像

[英]I upload an image to firebase storage but i can't see image in firebase storage

First this is about my uploadImage code, I try to fix it again and again but doesn't work首先这是关于我的 uploadImage 代码,我尝试一次又一次地修复它但不起作用

Future uploadImageFile() async
  {
    String fileName = DateTime.now().millisecondsSinceEpoch.toString();
    StorageReference storageReference = FirebaseStorage.instance.ref().child("Chat Images").child(fileName);

    StorageUploadTask storageUploadTask = storageReference.putFile(imageFile);
    StorageTaskSnapshot storageTaskSnapshot = await storageUploadTask.onComplete;

    storageTaskSnapshot.ref.getDownloadURL().then((downloadUrl){
      
      imageUrl = downloadUrl;
      setState(() {
        isLoading = false;
        onSendMessage(imageUrl, 1);
      });
    }, onError: (error){
      setState(() {
        isLoading = false;
      });
      Fluttertoast.showToast(msg: "Error: " + error);
    });
  }

And after in click upload image the result in terminal like this:在点击上传图片后,终端中的结果如下:

[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: type 'PlatformException' is not a subtype of type 'String'
#0      ChatScreenState.uploadImageFile.<anonymous closure> (package:telegramchatapp/Pages/ChattingPage.dart:726:47)
#1      _rootRunUnary (dart:async/zone.dart:1436:47)
#2      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
#3      _FutureListener.handleError (dart:async/future_impl.dart:178:22)
#4      Future._propagateToListeners.handleError (dart:async/future_impl.dart:779:47)
#5      Future._propagateToListeners (dart:async/future_impl.dart:800:13)
#6      Future._completeError (dart:async/future_impl.dart:610:5)
#7      _completeOnAsyncError (dart:async-patch/async_patch.dart:276:13)
#8      StorageReference.getDownloadURL (package:firebase_storage/src/storage_reference.dart)
<asynchronous suspension>
      

I don't know what to do.我不知道该怎么办。

I made a simple function to upload to Firebase Storage that works for me:我做了一个简单的 function 上传到 Firebase 适合我的存储:

storage.dart存储.dart

  Future upload({required String ref, required File file}) async {
    return await FirebaseStorage.instance.ref(ref).putFile(file).catchError(
          (e) => Utils.log(
            e,
            type: LogType.error,
          ),
        );
  }

  Future<String> downloadURL({required String ref}) async {
    return await FirebaseStorage.instance.ref(ref).getDownloadURL().catchError(
          (e) => Utils.log(
            e,
            type: LogType.error,
          ),
        );
  }

This Utils.log() is just a logger, you can use Toast if you want.这个Utils.log()只是一个记录器,你可以根据需要使用 Toast。 And this is how I use it:这就是我使用它的方式:

anywhere_else.dart: anywhere_else.dart:

  String ref = 'users/${Services.auth.currentUser()!.uid}/selfie.png';

  var upload = await Services.storage.upload(ref: ref, file: selfie);

  if (upload != null) {
    user.selfie = await Services.storage.downloadURL(ref: ref);
  }

The exception you show in your question comes from this line:您在问题中显示的异常来自这一行:

Fluttertoast.showToast(msg: "Error: " + error);

The error in here is not a string, so you can't concatenate it to a string.这里的error不是一个字符串,所以你不能把它连接成一个字符串。 What you want is:你想要的是:

Fluttertoast.showToast(msg: "Error: " + error.toString());

Or:或者:

Fluttertoast.showToast(msg: "Error: $error");

That will then show you what the root cause of the upload failure is, so you can address that.然后,这将向您显示上传失败的根本原因是什么,以便您解决该问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM