DEV Community

loading...
Cover image for Upload Image to Firebase Storage - Flutter (Android & Ios)

Upload Image to Firebase Storage - Flutter (Android & Ios)

rrtutors
I am an Android and Flutter Developer. Having 7+ Years of Experience in Mobile application development. Looking for article writers on my website.
Originally published at rrtutors.com Updated on ・2 min read

How to upload images to Firebase storage in Flutter

We can pick Image from Gallery or Capture image from Camera and upload it into Firebase.

Let's get started

Step 1: Create a Flutter application

Step 2: Configure Firebase application

Check the post how to configure Flutter application with Firebase

Step 3: Add require dependencies in pubspec.yaml file

dependencies:
  firebase_storage: ^3.0.8
  firebase_core: ^0.4.0+9
  firebase_analytics: ^5.0.6
  image_picker:
Enter fullscreen mode Exit fullscreen mode

Step 4:

Read Flutter How to pick Image from Camera or Gallery


final picker = ImagePicker();

  Future pickImage() async {
    final pickedFile = await picker.getImage(source: ImageSource.camera);

    setState(() {
      _imageFile = File(pickedFile.path);
    });
  }
Enter fullscreen mode Exit fullscreen mode

Step 5: Uploading Image to FirebaseStorage

Once an image is selected using ImagePicker, it will be displayed in the screen. Clicking on 'Upload Image' button on screen will upload the image to Firebase Storage. Make sure that FirebaseStorage write rules are configured appropriately. I'm temporarily enabling write access on FirebaseStorage access using allow write: if true;. Make sure to disable it once you've confirmed the functionality.

The following code will take the image file on device and upload it to the uploads folder in the FirebaseStorage

File _imageFile;


  Future uploadImageToFirebase(BuildContext context) async {
    String fileName = basename(_imageFile.path);
    StorageReference firebaseStorageRef =
        FirebaseStorage.instance.ref().child('uploads/$fileName');
    StorageUploadTask uploadTask = firebaseStorageRef.putFile(_imageFile);
    StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
    taskSnapshot.ref.getDownloadURL().then(
          (value) => print("Done: $value"),
        );
  }
Enter fullscreen mode Exit fullscreen mode

iOS configuration

Add permissions in iOS Info.list

<key>NSCameraUsageDescription</key>
<string>Need to access your camera to capture a photo add and update profile picture.</string>

<key>NSPhotoLibraryUsageDescription</key>
<string>Need to access your photo library to select a photo add and update profile picture</string>
Enter fullscreen mode Exit fullscreen mode

Download Flutter Upload Image to Firebase example

Discussion (0)