flutter filepicker 上传到 firebase 云存储不起作用

问题描述 投票:0回答:1
Future<String?> uploadImageOld () async {
  FilePickerResult? result = await FilePicker.platform.pickFiles();
  if (result != null) {
    final storageRef = FirebaseStorage.instance.ref();
    final images = storageRef.child('images');
    File file = File(result.files.single.path!);
    final filename = Path.basename(file.path);
    final uploadRef = images.child('images/$filename');
    await uploadRef.putFile(file);
    return await uploadRef.getDownloadURL();
  } else {
    return null;
  }
}

尝试选择一个文件并上传到 Firebase 云存储,但在选择文件后它冻结了。

flutter firebase dart firebase-storage filepicker
1个回答
0
投票

看看是否有效

Future<String?> uploadImageOld () async {
  FilePickerResult? result = await FilePicker.platform.pickFiles();
  if (result != null) {
    final metadata = SettableMetadata(contentType: "image/jpeg");// pass other meta data if needed
    final storageRef = FirebaseStorage.instance.ref();
   // final images = storageRef.child('images');
    File file = File(result.files.single.path!);
    final filename = Path.basename(file.path);
    final uploadRef =  storageRef.child('images/$filename');
   final uploadTask =await uploadRef.putFile(file);
   uploadTask.snapshotEvents.listen((TaskSnapshot taskSnapshot) {
    switch (taskSnapshot.state) {
      case TaskState.running:
        final progress =
            100.0 * (taskSnapshot.bytesTransferred / taskSnapshot.totalBytes);
        print("Upload is $progress% complete.");
        break;
      case TaskState.paused:
        print("Upload is paused.");
        break;
      case TaskState.canceled:
        print("Upload was canceled");
        break;
      case TaskState.error:
        // Handle unsuccessful uploads
        break;
      case TaskState.success:
        return await uploadRef.getDownloadURL();
        // Handle successful uploads on complete
        // ...
        break;
    }
  });
    //return await uploadRef.getDownloadURL();
  } else {
    return null;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.