【问题标题】:Uploading picture in Firebase storage flutter dart在 Firebase 存储中上传图片
【发布时间】:2021-12-23 03:11:07
【问题描述】:

我正在尝试从我的画廊上传图片,代码运行良好并且正在运行,但不在我的设备上。我想知道为什么它对我不起作用请帮忙 这是我尝试跑步时显示的内容。

图片的选择也很有效 名称和标题以及如果 Firebase 但图像在存储中不可见时显示的所有内容。

Future selectFile() async {
    final result = await FilePicker.platform
        .pickFiles(allowMultiple: false, type: FileType.any);

    if (result != null) {
      final path = result.files.single.path;
      file = File(path!);
      setState(() => file = File(path));
    }
  }

  Future uploadFile() async {
    if (file == null) return;

    final fileName = basename(file!.path);
    final destination = 'Content/$fileName';

    task = FirebaseApi.uploadFile(destination, file!);
    setState(() {});
  }

  Widget buildUploadStatus(UploadTask uploadTask) =>
      StreamBuilder<TaskSnapshot>(
        stream: task!.snapshotEvents,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            final snap = snapshot.data!;
            final progress = snap.bytesTransferred / snap.totalBytes;
            final percentage = (progress * 100).toStringAsFixed(2);

            return Row(
              children: [
              
                Text(
                  '$percentage %',
                  style: GoogleFonts.asap(
                    fontSize: 17,
                    color: Colors.white,
                    fontWeight: FontWeight.w500,
                  ),
                )
              ],
            );
          } else {
            return Container();
          }
        },
      );

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    throw UnimplementedError();
  }
}

class StatfulWidget {}

class FirebaseApi {
  static UploadTask? uploadFile(String destination, File file) {
    try {
      final ref = FirebaseStorage.instance.ref(destination);

      return ref.putFile(file);
    } on FirebaseException catch (e) {
      return null;
    }
  }
}

【问题讨论】:

  • 错误消息似乎来自 Firestore(文档数据库)而不是来自 Storage。它明确表示您正在为文档 ID 传递一个空字符串,因此我建议您检查一下。如果找不到,请编辑您的问题以显示导致此问题的代码。

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


【解决方案1】:

那是因为你给数据库一个空值。我遇到了类似的问题,我将最新消息发送到数据库并使用“。”对其进行初始化。相反,请使用空格“”。

示例代码: admin.firestore().collection('whatever').doc(""+id); 或使用用反引号括起来的 ${id} `

另一种解决方案可能是数据需要采用 JSON 格式。默认设置为文本,所以必须改为JSON(application/json)。

这是一个与同一问题相关的 Github 论坛:https://github.com/firebase/firebase-admin-node/issues/320

【讨论】:

    猜你喜欢
    • 2021-06-28
    • 2020-10-05
    • 2020-12-30
    • 2017-09-21
    • 2021-08-18
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    相关资源
    最近更新 更多