首先這是關于我的 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);
});
}, one rror: (error){
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: "Error: " error);
});
}
在點擊上傳圖片后,終端中的結果如下:
[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>
我不知道該怎么辦。
uj5u.com熱心網友回復:
我做了一個簡單的函式來上傳到適合我的 Firebase 存盤:
存盤.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,
),
);
}
這Utils.log()只是一個記錄器,您可以根據需要使用 Toast。這就是我使用它的方式:
任何地方_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);
}
uj5u.com熱心網友回復:
您在問題中顯示的例外來自這一行:
Fluttertoast.showToast(msg: "Error: " error);
此處的errorin 不是字串,因此您不能將其連接到字串。你想要的是:
Fluttertoast.showToast(msg: "Error: " error.toString());
要么:
Fluttertoast.showToast(msg: "Error: $error");
然后,這將向您顯示上傳失敗的根本原因是什么,以便您解決這個問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/444490.html
標籤:火力基地 扑 镖 firebase-存储
上一篇:不能無條件呼叫方法“map”,因為接收者可以為“null”
下一篇:如何在我的代碼中添加重置密碼功能
