DVCone
DVCone

Reputation: 109

Flutter : How to upload image to firestore in form

Hay, I'm new in firestore and a little bit confused about uploading data to firestore. let's take an example, I have a form format like this :

enter image description here

so, there is a data (name, DoB) form and upload image form. as far I know is to store data collection (like name and Date of Brith section) is using the cloud_firestore plugin, otherwise to store the image file is using firebase_store plugin.

then, how do I use both plugins at the same time when uploading forms like this? or is there another method to upload image with a form?

Thanks.

Upvotes: 1

Views: 2695

Answers (1)

griffins
griffins

Reputation: 8264

You need to first upload the image to firebse_storage , get a link and create the firestore document.

example

// upload the file helper
 Future<String?> uploadFile(FilePickerResult? fileName) async {
    Uint8List? fileBytes = fileName!.files.first.bytes;

    // Create a Reference to the file
    var filename = DateTime.now().toLocal().toIso8601String();
    if (kIsWeb) {
      firebase_storage.Reference ref = firebase_storage.FirebaseStorage.instance
          .refFromURL(storageRefFbS)
          .child("files")
          .child(filename + "." + fileName.files.first.extension!);
      await ref.putData(fileName.files.first.bytes!);

      var dwonloadurl = await ref.getDownloadURL();
      log(dwonloadurl);
      return dwonloadurl;
    }
  }

//upload a file/image

 var downloadUrl =
                          await firebaseHelper.uploadFile(myPickedFile);

// save the data

 FirebaseFirestore.instance
          .collection("myData")
          
          .set({
        "sname": "name",
        "url":downloadUrl,
        
        
      });

Upvotes: 2

Related Questions