[英]"Could not convert: Instance of '_Uri'" error while uploading image on cloud firestore's latest version on flutter web
使用最新版本的cloud_firestore: ^0.14.0+2
出現以下錯誤cloud_firestore: ^0.14.0+2
在雲cloud_firestore: ^0.14.0+2
上傳圖像時。
Uncaught (in promise) Error: [cloud_firestore/unknown] Invalid argument (dartObject): Could not convert: Instance of '_Uri'
圖像已成功上傳到存儲部分,但圖像鏈接未在 cloud firestore db 中更新。
下面是我從中選擇圖像並點擊他上傳 btn 上傳圖像的課程
class AddNewItemView extends StatefulWidget {
@override
_AddNewItemViewState createState() => _AddNewItemViewState();
}
class _AddNewItemViewState extends State<AddNewItemView> {
MediaInfo _itemPic;
@override
Widget build(BuildContext context) {
return Scaffold(
.....
child: RawMaterialButton(
onPressed: () async {
MediaInfo pickedImg = await ImagePickerWeb.getImageInfo; <---- image_picker_web package
setState(() {
_itemPic = pickedImg;
});
.........
FlatButton(
onPressed: () async {
bool addDataSuccess = await model.addNewItem( _itemPic);
if (addDataSuccess) {
print("Inside addDataSuccess");
} else {
print("Outside addDataSuccess");
}
},
),
.....
);
}
該類負責從uploadImageFile
函數中提取圖片uri並上傳到cloud firestore
。 與圖片一起傳遞的任何其他信息都會上傳到雲防火牆中,只有“圖片”沒有上傳
class ItemViewModel extends BaseViewModel {
Future<bool> addNewItem( MediaInfo pic ) async {
Uri _uploadedImgURL = await ConstantFtns()
.uploadImageFile(pic, "ImgPathString", "fileName");
await FirebaseFirestore.instance.collection('item').doc("tempID").set(
{
'pic': _uploadedImgURL,
'otherInfo': 'otherTempInfo'
},
);
}
在下面的課程中,我得到了有效的imageUri
沒有任何錯誤,圖像被上傳到存儲部分,唯一的問題是它沒有被上傳到雲 Firestore 數據庫中
import 'package:firebase/firebase.dart' as fb;
import 'package:mime_type/mime_type.dart';
import 'package:path/path.dart' as p;
class ConstantFtns {
Future<Uri> uploadImageFile(
MediaInfo mediaInfo, String ref, String fileName) async {
try {
String mimeType = mime(p.basename(mediaInfo.fileName));
final String extension = extensionFromMime(mimeType);
var metadata = fb.UploadMetadata(
contentType: mimeType,
);
fb.StorageReference storageReference =
fb.storage().ref(ref).child(fileName + ".$extension");
fb.UploadTaskSnapshot uploadTaskSnapshot =
await storageReference.put(mediaInfo.data, metadata).future;
Uri imageUri = await uploadTaskSnapshot.ref.getDownloadURL();
print("download url $imageUri"); <---- IT GETS P[RINTED AND IT SHOWED VALED IMAGE URL STORED IN STORAGE
return imageUri;
} catch (e) {
print("File Upload Error $e");
return null;
}
}
}
由於您想要的只是將 URL 存儲到 firestore 中,因此您實際上並不需要將getDownloadURL()
值作為Uri
對象,您只需要它的字符串值,這是您收到轉換錯誤的錯誤,我建議您遵循此社區答案中的建議,這樣您的代碼將如下所示:
String uploadImageFile(
MediaInfo mediaInfo, String ref, String fileName) async {
try {
...
var imageUri = await uploadTaskSnapshot.ref.getDownloadURL() as String;
print("download url $imageUri"); <---- IT GETS P[RINTED AND IT SHOWED VALED IMAGE URL STORED IN STORAGE
return imageUri
} catch (e) {
...
}
}
})
您還必須將ItemViewModel
類中的調用更改為:
String _uploadedImgURL = await ConstantFtns()
.uploadImageFile(pic, "ImgPathString", "fileName");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.