Home > Software design >  Image URL from firebase storage to firestore
Image URL from firebase storage to firestore

Time:06-27

this is how I upload images to firebase storage and get the Download URL in firebase Firestore. Everything works properly how ever I get the 1st URL but not the Second one.

Future<void> uploadImage2(image2) async {
    setState(() {
      isLoader2 = true;
    });
    final bytess = image2.readAsBytesSync();
    var timeStamp = DateTime.now();
    final metadata = firebase_storage.SettableMetadata(contentType: 'CarImage');
    firebase_storage.UploadTask task = firebase_storage.FirebaseStorage.instance
        .ref('Toyota-Images/$timeStamp/2.png')
        .putData(bytess, metadata);
    firebase_storage.TaskSnapshot downloadUrl2 = (await task);

    String url = (await downloadUrl2.ref
        .getDownloadURL()); //this is the url of uploaded image
    imageUrl2 = url;
    setState(() {
      isLoader2 = false;
    });
  }

  Future<void> uploadImage3(image3) async {
    setState(() {
      isLoader3 = true;
    });
    final bytess = image3.readAsBytesSync();
    var timeStamp = DateTime.now();
    final metadata = firebase_storage.SettableMetadata(contentType: 'CarImage');
    firebase_storage.UploadTask task = firebase_storage.FirebaseStorage.instance
        .ref('Toyota-Images/$timeStamp.png')
        .putData(bytess, metadata);
    firebase_storage.TaskSnapshot downloadUrl3 = (await task);

    String url = (await downloadUrl3.ref
        .getDownloadURL()); //this is the url of uploaded image
    imageUrl3 = url;
    setState(() {
      isLoader3 = false;
    });
  }

CodePudding user response:

You can upload image to firebase as below

First of all you need to add this plugin in pubspec.yaml

firebase_storage: ^8.0.0

import 'package:firebase_storage/firebase_storage.dart' as firebase_storage;

Future<void> uploadFile(File _image) async {
    setState(() {
      isLoader = true;
    });
    final bytess = _image.readAsBytesSync();  //"_image" is your selected image or any other which you need to upload
    var timeStamp = DateTime.now();
    final metadata = firebase_storage.SettableMetadata(contentType: 'image/jpeg');
    firebase_storage.UploadTask task = firebase_storage.FirebaseStorage.instance
        .ref('cover_photo/' timeStamp.toString() 'insp_cover_photo.png').putData(bytess,metadata);
    firebase_storage.TaskSnapshot  downloadUrl = (await task);

    String url = (await downloadUrl.ref.getDownloadURL()); //this is the url of uploaded image
    setState(() {
      isLoader = false;
    });
}

Let me know if you have any questions

CodePudding user response:

You can do it using firebase_storage.

you can get url by using this function.

  Future<String> uploadFile(File _imageFile) async {
    String fileName = DateTime.now().millisecondsSinceEpoch.toString();
    Reference reference = FirebaseStorage.instance.ref().child(fileName);
    UploadTask uploadTask = reference.putFile(_imageFile);
    return uploadTask.then((TaskSnapshot storageTaskSnapshot) {
      return storageTaskSnapshot.ref.getDownloadURL();
    }, one rror: (e) {
      throw Exception(e.toString());
    });
  }
  • Related