Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Upload Image to Firebase Storage

Tags:

I get an image from Firebase Storage, take a photo with the camera, or pick one from the Library. When one of these is complete, I have a class which stores an Image so that I can use it when required.

Now I need to upload this image to Firebase Storage (modified or new, doesn't matter). Firebase allows me to use one of the following: putData or putFile, each needing either an Uint8List or File respectively.

How can I take my Image and either get a File or Uint8List from it for uploading?

--

Alternatively to remedy this, how can I get a File of my image when I retrieve it from Firebase Storage?

Either way results in providing the correct data type to upload the image, be it get a File at the start or end.

like image 682
Josh Kahane Avatar asked Aug 15 '18 11:08

Josh Kahane


People also ask

How do I upload images to Firebase storage Flutter?

The idea is to create a simple interface containing an image display container and a button to upload an image after selecting it from the gallery. After an image is selected, the file will be automatically uploaded to the Firebase storage and then displayed on the screen.

Can you store images in Firebase?

The Firebase SDKs for Cloud Storage add Google security to file uploads and downloads for your Firebase apps, regardless of network quality. You can use our SDKs to store images, audio, video, or other user-generated content.


1 Answers

When you select an image with the image picker, it returns a File, you can use await to wait until the user selects a file then store it as a File. Here is some sample code of how you can get the file from the image picker and upload it to Firebase.

    FirebaseStorage _storage = FirebaseStorage.instance;      Future<Uri> uploadPic() async {      //Get the file from the image picker and store it      File image = await ImagePicker.pickImage(source: ImageSource.gallery);        //Create a reference to the location you want to upload to in firebase       StorageReference reference = _storage.ref().child("images/");      //Upload the file to firebase      StorageUploadTask uploadTask = reference.putFile(file);      // Waits till the file is uploaded then stores the download url      Uri location = (await uploadTask.future).downloadUrl;      //returns the download url      return location;    } 
like image 72
Nash Avatar answered Oct 09 '22 21:10

Nash