无法使用Flutter将文件上传到FirebaseStorage

时间:2019-12-28 12:10:56

标签: android firebase flutter dart firebase-storage

我在以File对象的形式将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});
}

StackTrace

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 个答案:

答案 0 :(得分:2)

child()添加到StorageReference,例如:

StorageReference reference = _storage.ref().child("images");

您需要向Firebase存储添加路径,因为您无法将数据写入存储的根目录。因此,您可以在child()方法内添加路径。