我已经成功地使用multi_image_picker获得了多个图像,但是未能将图像上传到Firebase存储。我正在寻求有关将图像保存到Firebase存储并返回url的帮助。
我已经在下面发布了我的代码,谢谢。
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/services.dart';
import 'package:multi_image_picker/multi_image_picker.dart';
import 'package:mock_data/mock_data.dart';
class PageTwo extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<PageTwo> {
List<Asset> images = List<Asset>();
String popop = mockString(50);
@override
void initState() {
super.initState();
}
Widget buildGridView() {
return GridView.count(
crossAxisCount: 3,
children: List.generate(images.length, (index) {
Asset asset = images[index];
return AssetThumb(
asset: asset,
width: 300,
height: 300,
);
}),
);
}
Future saveImage(Asset asset) async {
String fileName = popop;
ByteData byteData = await asset.requestOriginal();
List<int> imageData = byteData.buffer.asUint8List();
StorageReference ref = FirebaseStorage.instance.ref().child(fileName);
StorageUploadTask uploadTask = ref.putData(imageData);
return await (await uploadTask.onComplete).ref.getDownloadURL();
}
Future<void> loadAssets() async {
List<Asset> resultList = List<Asset>();
try {
resultList = await MultiImagePicker.pickImages(
maxImages: 300,
enableCamera: true,
selectedAssets: images,
cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
materialOptions: MaterialOptions(
actionBarColor: "#abcdef",
actionBarTitle: "Example App",
allViewTitle: "All Photos",
selectCircleStrokeColor: "#000000",
));
}
}
if (!mounted) return;
setState(() {
images = resultList;
_error = error;
});
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Getting pics'),
),
body: Column(
children: <Widget>[
RaisedButton(
child: Text("Pick images"),
onPressed: loadAssets,
),
RaisedButton(
child: Text("Save Image"),
onPressed: () => saveImage,
),
Expanded(
child: buildGridView(),
)
],
),
),
);
}
}
答案 0 :(得分:0)
您似乎并没有对URL进行任何操作。 saveFile上的Future不会返回任何内容,也不会将结果放在任何地方。
await (await uploadTask.onComplete).ref.getDownloadURL()
加上,等待等待看起来有些奇怪。您可以通过简单地级联onComplete等待来摆脱困境。我不确定,但值得一试。
await uploadTask.onComplete..ref.getDownloadURL()
此外,当我使用StorageUploadTask时,我通常会监听任务以获取上传进度,然后添加onComplete部分以获取downloadURL。