【问题标题】:Upload image picked from Flutter web to firebase storage and to Firestore将从 Flutter Web 中挑选的图像上传到 Firebase 存储和 Firestore
【发布时间】:2021-03-29 19:15:44
【问题描述】:

我能够使用代码从 Flutter Web 中获取图像:

Uint8List uploadedImage;


  _startFilePicker() async {
    InputElement uploadInput = FileUploadInputElement();
    uploadInput.click();

    uploadInput.onChange.listen((e) {
      // read file content as dataURL
      final files = uploadInput.files;
      if (files.length == 1) {
        final file = files[0];
        FileReader reader =  FileReader();

        reader.onLoadEnd.listen((e) {
          setState(() {
            uploadedImage = reader.result;
          });
        });

        reader.onError.listen((fileEvent) {
          setState(() {
            Text( "Some Error occured while reading the file");
          });
        });

        reader.readAsArrayBuffer(file);
      }
    });
  }

  Widget uploadImage() {
    return Container(
      width: 530,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          uploadedImage == null ? CircleAvatar(
            radius: 60,
            backgroundColor: Colors.grey[100],
            backgroundImage: AssetImage('assets/images/profileavatar.png'),
          ):
            CircleAvatar(
              radius: 65,
              backgroundImage: AssetImage('assets/images/backgroundslide.gif'),
              child: CircleAvatar(
                radius: 60,
                backgroundImage: MemoryImage(uploadedImage),
              ),
            )  ,
          SizedBox(
            width: 20,
          ),
          uploadedImage == null ? RaisedButton(
            color: Colors.orange,
            onPressed: () {
              _startFilePicker();
            },
            child: Text(
              'Aggiungi un immagine profilo',
              style: TextStyle(color: Colors.white, fontSize: 12),
            ),
          ): RaisedButton(
            color: Colors.orange,
            onPressed: () {
              _startFilePicker();
            },
            child: Text(
              'Modifica immagine profilo',
              style: TextStyle(color: Colors.white, fontSize: 12),
            ),
          ),

        ],
      ),
    );
  }

通过这种方式,我成功地从桌面获得了图像。 现在我需要将这张图片上传到 Flutter 中的存储并进入 Firestore 中的 Collection:

var firebaseUser =  FirebaseAuth.instance.currentUser;

  Future<Uri> uploadImageFile(html.File uploadedImage,
      {String imageName}) async {
    fb.StorageReference storageRef = fb.storage().ref('images/$imageName');
    fb.UploadTaskSnapshot uploadTaskSnapshot = await storageRef.put(uploadedImage).future;
    await FirebaseFirestore.instance.collection('ifUser').doc(firebaseUser.uid)
        .update({"avatarImage": uploadImageFile(uploadedImage),});
    Uri imageUri = await uploadTaskSnapshot.ref.getDownloadURL();
    return imageUri;
  }

当我调用函数uploadImageFile(uploadedImage); 我得到错误:

参数类型“Uint8List”不能分配给参数类型“文件”。 这是正确的方法吗?

【问题讨论】:

    标签: flutter google-cloud-firestore google-cloud-storage firebase-storage flutter-web


    【解决方案1】:

    从错误看来,上传需要一个文件,而您正在传递 Uint8List。

    您可以尝试使用this 转换数据,然后再将其上传到存储。

    File.fromRawPath(Uint8List uint8List);

    【讨论】:

    • 你能给我一个例子如何编码吗?
    • 我没有重现这个问题,但是上面的命令是用于从 Unit8List 生成一个文件。我还找到了this Stackoverflow 帖子,这可能会对您有所帮助。
    猜你喜欢
    • 2021-08-11
    • 2021-07-28
    • 2020-04-30
    • 2018-10-24
    • 2019-01-22
    • 2021-10-14
    • 2021-03-19
    • 2020-12-20
    相关资源
    最近更新 更多