gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:25:07 25 4
gpt4 key购买 nike

我在将 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});
}

堆栈跟踪

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)

最佳答案

child()添加到StorageReference,例如:

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

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

关于android - 无法使用 Flutter 将文件上传到 FirebaseStorage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59510759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com