firebase Flutter-Firestore:-无法上传图像到firestore,它说没有找到参考对象

cvxl0en2  于 2022-11-17  发布在  Flutter
关注(0)|答案(1)|浏览(103)

我对飞镖和一般的编码都很陌生。我在YouTube上看了教程后产生了这个代码。在大多数情况下,我已经能够自己解决我的大多数问题,在这里我觉得我需要一些帮助。我已经写了上传照片的代码,但是它给出了下面的错误。请帮助我理解这一点。
I/扑动(7512):[firebase_storage/object-not-found]在所需的引用处不存在对象。
我的代码是:-

Future uploadImagetoFB() async {
        if (_image != null) {
         try{
           String fileName = Path.basename(_image.path);
           print('The uploaded file name is: ${fileName}');
           final Reference storageReference =
           FirebaseStorage.instance.ref().child('profileImages');
           _imageUrl = await storageReference.getDownloadURL();
           print('The Image Url: $_imageUrl');
         } catch (e){
           print(e);
         }
        }
      }
    
      Future PickedImage(ImageSource source) async {
        try {
          final image = await ImagePicker()
              .pickImage(source: source, maxWidth: 160, maxHeight: 160);
          if (image == null) return;
          setState(() {
            _image = File(image.path);
          });
          uploadImagetoFB();
        } on PlatformException catch (e) {
          print('failed to Upload ');
        }
      }

kupeojn6

kupeojn61#

要将照片上传到Firebase,请使用以下代码。

这是我设置图像URL的字符串:

String? url;

图像上传代码:

uploadImage() async {
    final _firebaseStorage = FirebaseStorage.instance;
    final _imagePicker = ImagePicker();
    PickedFile image;

    //Check Permissions
    await Permission.photos.request();
    var permissionStatus = await Permission.photos.status;
    if (permissionStatus.isGranted) {
      //Select Image
      image = (await _imagePicker.getImage(source: ImageSource.gallery))!;
      var file = File(image.path);
      int uploadTimestamp = DateTime.now().millisecondsSinceEpoch;
      if (image != (null)) {
        Reference ref =
            _firebaseStorage.ref().child('profileImages/$uploadTimestamp');
        UploadTask uploadTask = ref.putFile(file);

        var imageUrl = await (await uploadTask).ref.getDownloadURL();
        setState(() {
          url = imageUrl.toString();
        });
      } else {
        print('No Image Path Received');
      }
    } else {
      print('Permission not granted. Try Again with permission access');
    }
  }

Firebase的结果:
“描述文件图像”文件夹:

上传的图像:

相关问题