我成功地從畫廊中挑選了多張圖片,并決定嘗試上傳到 firebase 并檢索圖片 URL。呼叫上傳后,它請求傳遞一個我所做的引數,但是當我嘗試指定傳遞它的引數的索引時,它給出了一個錯誤“引數型別'串列'不能分配給引數型別'資產”僅使用該特定索引保存該影像。
我也嘗試了一個 for 回圈,但它仍然只保存一個影像并回傳它的 url。
我如何上傳所有影像 下面是我如何加載影像。
Future<void> loadAssets() async {
List<Asset> resultList = <Asset>[];
resultList = await MultiImagePicker.pickImages(
maxImages: 300,
enableCamera: true,
selectedAssets: images,
cupertinoOptions: const CupertinoOptions(takePhotoIcon: "chat"),
materialOptions: const MaterialOptions(
actionBarColor: "#abcdef",
actionBarTitle: "Example App",
allViewTitle: "All Photos",
selectCircleStrokeColor: "#000000",
));
if (!mounted) return;
setState(() {
images = resultList;
});
}
然后我使用以下代碼段上傳影像
Future UploadImage(Asset asset) async {
String fileName = popop;
ByteData byteData = await asset.getByteData();
List<int> imageData = byteData.buffer.asUint8List();
Reference ref = FirebaseStorage.instance.ref().child(fileName);
UploadTask uploadTask = ref.putData(Uint8List.fromList(imageData));
TaskSnapshot snapshot= await uploadTask;
String url= await snapshot.ref.getDownloadURL();
if (kDebugMode) {
print(url);
}
/// After this Update user Profile or add url to collection
return url;
使用下面的代碼呼叫/上傳串列中的單個影像
RaisedButton(
child: const Text("Save Image"),
onPressed: () => UploadImage(images[0]),
),
我如何上傳所有圖片。我什至嘗試了下面的 for 回圈
for(int i=o; i<images.length;i ){
UploadImages(images[i]);
}
但只上傳了一張圖片
uj5u.com熱心網友回復:
您可以像這樣迭代影像,例如:
final fileNames = <String>[];
for (final image in images) {
final fileName = await uploadImage(image);
fileNames.add(fileName);
}
不確定您是否真的需要呼叫getDownloadUrl()(取決于您的用例),但此方法會創建一個公共且長期存在的 url。
例如,如果您只想在 Cloud Firestore 中存盤參考,則可以從函式中獲取相應檔案的名稱/完整路徑,TaskSnapshot如下uploadImage所示:
Future<String> UploadImage(Asset asset) async {
String fileName = popop;
ByteData byteData = await asset.getByteData();
List<int> imageData = byteData.buffer.asUint8List();
Reference ref = FirebaseStorage.instance.ref().child(fileName);
UploadTask uploadTask = ref.putData(Uint8List.fromList(imageData));
TaskSnapshot snapshot= await uploadTask;
// Assuming you are interested in the fullPath use snapshot.ref.fullPath
// For the name use snapshot.ref.name instead
return snapshot.ref.fullPath
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/442460.html
