文件未上传到Firebase存储

问题描述 投票:0回答:1

更新:较小的文件(<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)有相同的问题。

并且_img和_file在onPressed中用以下代码设置了两个不同的按钮:

_img = await FilePicker.getFilePath(type: FileType.ANY);

_file.add(await FilePicker.getFilePath(type: FileType.ANY));

[file来自FilePicker.getFilePath(type: FileType.ANY),但我没有得到任何错误,所以我知道该文件存在。

更新:较小的文件(<1 MB)似乎工作正常。我正在尝试从flutter应用程序将文件上传到Firebase Storage,但是该文件未上传,并且uploadTask.onComplete完成...

firebase flutter dart firebase-storage
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.