无法使用 Flutter 将文件上传到 FirebaseStorage
Posted
技术标签:
【中文标题】无法使用 Flutter 将文件上传到 FirebaseStorage【英文标题】:Unable to upload File to FirebaseStorage using Flutter 【发布时间】:2020-04-18 00:21:19 【问题描述】:我在将文件对象形式的 jpg 图像上传到 FirebaseStorage 时遇到问题。错误发生在这一行StorageUploadTask uploadTask = reference.putFile(image)
。我到处搜索,没有找到任何关于Unhandled Exception: PlatformException
的信息。
代码:
void uploadAd(File image,int price, String name,String desc,String location,String category) async
print(image.path);
String userid = await UserData().getId();
FirebaseStorage _storage = FirebaseStorage.instance;
StorageReference reference = _storage.ref();
StorageUploadTask uploadTask = reference.putFile(image); //Error occurs on this line
var refurl = (await uploadTask.onComplete).ref.getDownloadURL();
print(refurl);
String url = refurl.toString();
String productid = Uuid().v4();
Firestore.instance.collection('products').add('product_id': productid, 'user_id': userid,'name':name,'price':price,'location':location,'category':category,'primary_image':url,'description':desc);
堆栈跟踪
I/flutter (25579): before upload function /storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20191228-WA0000.jpg <-- path of file being uploaded
E/flutter (25579): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, childName cannot be null or empty, null)
E/flutter (25579): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter (25579): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:321:33)
E/flutter (25579): <asynchronous suspension>
E/flutter (25579): #2 _StorageFileUploadTask._platformStart (package:firebase_storage/src/upload_task.dart:130:36)
E/flutter (25579): #3 StorageUploadTask._start (package:firebase_storage/src/upload_task.dart:35:21)
E/flutter (25579): #4 StorageReference.putFile (package:firebase_storage/src/storage_reference.dart:65:10)
E/flutter (25579): #5 uploadAd (package:second_valet/datalogic.dart:15:44)
E/flutter (25579): #6 _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:73:64)
E/flutter (25579): #7 _rootRunUnary (dart:async/zone.dart:1134:38)
E/flutter (25579): #8 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter (25579): #9 _FutureListener.handleValue (dart:async/future_impl.dart:139:18)
E/flutter (25579): #10 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:680:45)
E/flutter (25579): #11 Future._propagateToListeners (dart:async/future_impl.dart:709:32)
E/flutter (25579): #12 Future._completeWithValue (dart:async/future_impl.dart:524:5)
E/flutter (25579): #13 _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:32:15)
E/flutter (25579): #14 _completeOnAsyncReturn (dart:async-patch/async_patch.dart:290:13)
E/flutter (25579): #15 UserData.getId (package:second_valet/userdata.dart)
E/flutter (25579): #16 _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:73:64)
E/flutter (25579): #17 _rootRunUnary (dart:async/zone.dart:1134:38)
E/flutter (25579): #18 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter (25579): #19 _FutureListener.handleValue (dart:async/future_impl.dart:139:18)
E/flutter (25579): #20 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:680:45)
E/flutter (25579): #21 Future._propagateToListeners (dart:async/future_impl.dart:709:32)
E/flutter (25579): #22 Future._addListener.<anonymous closure> (dart:async/future_impl.dart:389:9)
E/flutter (25579): #23 _rootRun (dart:async/zone.dart:1126:13)
E/flutter (25579): #24 _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (25579): #25 _CustomZone.runGuarded (dart:async/zone.dart:925:7)
E/flutter (25579): #26 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
E/flutter (25579): #27 _microtaskLoop (dart:async/schedule_microtask.dart:43:21)
E/flutter (25579): #28 _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)
【问题讨论】:
【参考方案1】:在StorageReference
中添加child()
,例如:
StorageReference reference = _storage.ref().child("images");
您需要添加到 Firebase 存储的路径,因为您无法将数据写入存储的根目录。因此,您可以在 child()
方法中添加您的路径。
【讨论】:
以上是关于无法使用 Flutter 将文件上传到 FirebaseStorage的主要内容,如果未能解决你的问题,请参考以下文章
Flutter:如何将pdf文件上传到firebase存储?
无法使用 Flutter Mobile 上传到 Firebase 存储
Flutter web - 无法将图像上传到 Firebase 存储