更新:较小的文件(<1 MB)似乎可以正常工作。
我正在尝试将文件从flutter应用程序上载到Firebase Storage,但是该文件没有上载,uploadTask.onComplete立即完成。在尝试使用return (await storageRef.getDownloadURL());
获取URL之前,我不会出错。
以下是相关代码:
_uploadFile(file) async {
String fileName = DateFormat('yy-MM-ddTH:mm:s').format(DateTime.now()).toString() + file.split('/').last;
StorageReference storageRef = FirebaseStorage.instance.ref().child(fileName);
StorageUploadTask uploadTask = storageRef.putFile(File(file));
await uploadTask.onComplete;
return (await storageRef.getDownloadURL());
}
错误:
I/flutter (20165): PlatformException(download_error, Object does not exist at location., null)
E/flutter (20165): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(download_error, Object does not exist at location., null)
E/flutter (20165): #0 _AddNewsState._uploadFile (package:ny_skolapp/add_news.dart:39:7)
E/flutter (20165): <asynchronous suspension>
E/flutter (20165): #1 _AddNewsState._uploadNews (package:ny_skolapp/add_news.dart:49:25)
E/flutter (20165): #2 _AddNewsState.build.<anonymous closure> (package:ny_skolapp/add_news.dart:329:31)
E/flutter (20165): #3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
E/flutter (20165): #4 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:789:36)
E/flutter (20165): #5 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (20165): #6 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11)
E/flutter (20165): #7 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5)
E/flutter (20165): #8 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:199:7)
E/flutter (20165): #9 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:467:9)
E/flutter (20165): #10 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:76:12)
E/flutter (20165): #11 PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:117:9)
E/flutter (20165): #12 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8)
E/flutter (20165): #13 PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:115:18)
E/flutter (20165): #14 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:7)
E/flutter (20165): #15 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
E/flutter (20165): #16 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (20165): #17 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (20165): #18 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (20165): #19 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (20165): #20 _rootRunUnary (dart:async/zone.dart:1138:13)
E/flutter (20165): #21 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter (20165): #22 _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter (20165): #23 _invoke1 (dart:ui/hooks.dart:273:10)
E/flutter (20165): #24 _dispatchPointerDataPacket (dart:ui/hooks.dart:182:5)
E/flutter (20165):
调用_uploadFile(file)的代码如下:
_uploadNews() async {
setState(() {
_uploading = true;
});
if (_img != null) {
if (!_img.contains('http')) {
_imgUrl = await _uploadFile(_img);
}else{
_imgUrl = _img;
}
}
if (_file.isNotEmpty) {
for (int i = 0; i < _file.length; i++) {
if (!_file[i].contains('http')) {
_fileUrl.add(await _uploadFile(_file[i]));
}else{
_fileUrl.add(_file[i]);
}
}
}
await Firestore.instance
.collection('news')
.document('$_title ${DateFormat('yyyy-MM-dd kk:mm').format(DateTime.now())}')
.setData({
'title': _title,
'content': _content ?? '',
'time': Timestamp.now(),
'img': _imgUrl ?? '',
'files': _fileUrl ?? [],
'fileNames': _fileName ?? [],
});
setState(() {
_uploading = false;
});
Navigator.of(context).pop();
}
_uploadNews(_img)
和_uploadNews(_file)
有相同的问题。
在onPressed
中,使用以下代码用两个不同的按钮设置_img和_file:
_img = await FilePicker.getFilePath(type: FileType.ANY);
和
_file.add(await FilePicker.getFilePath(type: FileType.ANY));
file
来自FilePicker.getFilePath(type: FileType.ANY)
,但我没有得到任何错误,所以我知道该文件存在。
答案 0 :(得分:0)
首先,您说使用return (await storageRef.getDownloadURL());
时遇到错误,可以确定是哪个错误。
然后,您可以尝试使用类似的代码来上传文件并捕获错误:
Future<String> _uploadFile(String filePath) async {
try {
final String fileName = DateFormat('yy-MM-ddTH:mm:s').format(DateTime.now()).toString() + filePath.split('/').last;
final StorageReference storageRef = FirebaseStorage.instance.ref().child(fileName);
final StorageUploadTask task = storageRef.putFile(File(filePath));
return await (await task.onComplete).ref.getDownloadURL();
} catch (error) {
print(error.toString());
throw error.toString();
}
}
我使用了与此代码相似的代码,直到现在一直有效。