void validateAndUpload() async {
if (_formKey.currentState.validate()) {
setState(() => isLoading = true);
if (_image1 != null) {
if (selectedSizes.isNotEmpty) {
String imageUrl1;
final FirebaseStorage storage = FirebaseStorage.instance;
final String picture1 =
"${DateTime.now().millisecondsSinceEpoch.toString()}.jpg";
StorageUploadTask task1 =
storage.ref().child(picture1).putFile(_image1);
task1.onComplete.then((snapshot1) async {
imageUrl1 = await snapshot1.ref.getDownloadURL();
_productServices.uploadProduct(
productName: productNameController.text,
brandName: _currentBrand,
details: detailController.text,
category: _currentCategory,
quantity: int.parse(quantityController.text),
size: selectedSizes,
picture: imageUrl1,
feature: feature,
sale: sale,
price: double.parse(priceController.text));
_formKey.currentState.reset();
getter 'onComplete'没有为'UploadTask'类型定义。(文档)尝试导入定义'onComplete'的库,将名称修改为现有getter的名称,或者定义一个名为'onComplete'的getter或字段。
这个错误似乎是正确的。你是说whenComplete
吗?
我通常更喜欢简单地await
任务:
var ref = storage.ref().child(picture1);
await ref.putFile(_image1);
imageUrl1 = await ref.getDownloadURL();
...
final ref = FirebaseStorage.instance
.ref("${DateTime.now().millisecondsSinceEpoch.toString()}.jpg");
var uploadEvent = ref.putFile(_image1!);
String imageUrl = await (await uploadEvent.whenComplete(() => null))
.ref
.getDownloadURL();