0

I need to link a bunch of images to a job, below is the snip that I am using:

    var jobId = 'BzQxgzJ5oYTn8bSEJ4l5';

    List filesList = [];
    for (var _photo in _imageFileList!) {
      final destination = 'JobImages/$jobId/';
      final ref = FirebaseStorage.instance.ref(destination);
      var file = File(_photo.path);
      await ref.putFile(file);
      var imageUrl = await ref.getDownloadURL();
      filesList.add(imageUrl);
    }

The issue that I am having is that I only see one destination: 'JobImages/$jobId/'. The reason why that happens is that the 'destination' variable is not changing. Now I can add a random string at the end of 'destination' but I don't want to do that because that might cause a destination replacement to occur in the future

so I need a way to get the current file names on the server so that I don't double-dip when uploading the new images. how do I do that? or is there a better way to do this?

Juan Casas
  • 268
  • 2
  • 13

1 Answers1

0

There are a few things that you can do in this situation. Below I have listed them in no particular order.

  1. You can use a hash of the file prior to uploading. A hash would generate a unique file name for each file you upload and if the file with the same file name already exists, you would merely overwrite the file with the same exact file. Here is the crypto package for hashing.

  2. You can use firestore to store a unique guid in a table and then use that ID to push new records to the table. This was answered by puf here.

  3. If naming is important, you could also store the names as they are uploaded into the storage bucket in firestore and then query firestore to determine if that name is already taken and then present an option for the user to rename the file.

Alexander N.
  • 1,458
  • 14
  • 25