Upload image to Firebase Storage Flutter Web

Viewed 174

I'm migrating my app to web, I when I try to upload any image to firebase storage I'm not being able.

class FirebaseApi {
  static UploadTask? uploadFile(String destinaiton, File file) {
    try {
      final ref = FirebaseStorage.instance.ref(destinaiton);
      return ref.putFile(file);
    } on FirebaseException catch (_) {
      return null;
    }
  }
}

And

 File? file;
  UploadTask? task;


  Future selectFile() async {
    final result = await ImagePicker().pickImage(source: ImageSource.gallery);
    if (result == null) return;
    setState(() => file = File(result.path));
  }

  Future uploadFile(String userId) async {
    if (file == null) return;
    final fileName = path.basename(file!.path);
    final destination = 'users/$userId/$fileName';

    task = FirebaseApi.uploadFile(destination, file!);
    if (task == null) return;
    final snapshot = await task!.whenComplete(() {});
    final urlDownload = await snapshot.ref.getDownloadURL();
    setState(() {
      imageUrl = urlDownload;
    });
  }

Would there be a way to integrate both app and web?

0 Answers
Related