skip to Main Content

I am trying to do the following:

  1. Take a web URL for an image and upload it into firebase
  2. Once the image is uploaded return the firebase URL for the image
Future<String> portrait(String imageinput) async {
  // Add your function code here!

  final response = await http.get(Uri.parse(imageinput));

  final imageBytes = response.bodyBytes;

  final storageRef = FirebaseStorage.instance
      .ref()
      .child('images/${DateTime.now().toString()}.jpg');

  final uploadTask = storageRef.putData(imageBytes);
  await uploadTask;

  final String imageUrl = await storageRef.getDownloadURL();
  return imageUrl;
}

2

Answers


  1. Please follow the below code , use with your requirement

    First, follow sendImage method then storage method then chatservice

    class StorageMethods {
      static final FirebaseFirestore firestore = FirebaseFirestore.instance;
    
      Reference _storageReference;
    
      Future<Map<String, dynamic>> uploadImageToStorage(File imageFile, String fileName) async {
        try {
          _storageReference = FirebaseStorage.instance.ref().child(fileName);
          UploadTask storageUploadTask = _storageReference.putFile(imageFile);
          var url = await (await storageUploadTask.whenComplete(() {})).ref.getDownloadURL();
          var name = _storageReference.name;
          print("image name ======>> $name");
          print("image url ======>> $url");
    
          return {"url": url, "name": name};
    
          return {"url": url, "name": name};
        } catch (e) {
          print("uploadImageToStorage exception ======>> $e");
          return null;
        }
      }
    
      Future<Map<String, dynamic>> uploadImage({
        @required String type,
        @required String fileName,
        @required File file,
        @required String receiverId,
        @required String senderId,
        @required ChatImageUploadProvider imageUploadProvider,
        @required String sender,
        String senderProfile,
        var numberOfMessage,
      }) async {
        // Set some loading value to db and show it to user
        imageUploadProvider.setToLoading();
    
        // Get url from the image bucket
        Map<String, dynamic> uploadMap = await uploadImageToStorage(file, fileName);
    
        // Hide loading
        imageUploadProvider.setToIdle();
    
        chatService.setImageMsg(
          type,
          uploadMap["url"],
          receiverId,
          senderId,
          uploadMap["name"],
          sender,
          senderProfile,
          numberOfMessage,
        );
    
        return uploadMap;
      }
    

    Inside the chatService method :

    Future<void> setImageMsg(String fileType, String url, String receiverId, String senderId, String bucketName,
          String username, String senderProfile, var numberOfMessage) async {
        Message message;
    
        message = Message.imageMessage(
            message: fileType == "image" ? "Photo Message" : "DOCUMENT",
            receiverId: receiverId,
            senderId: senderId,
            senderProfile: senderProfile,
            fileUrl: url,
            timestamp: Timestamp.now(),
            type: fileType,
            bucketName: bucketName,
            numberOfMessage: numberOfMessage);
    
        var map = message.toImageMap();
    
        await _messageCollection.doc(message.senderId).collection(message.receiverId).add(map);
    
        _messageCollection.doc(message.receiverId).collection(message.senderId).add(map);
      }
    

    This is my sendImage method

    void sendImage(String fileType, String fileName, String filePath) async {
        try {
          File selectedFile = File(filePath);
          var uploadMap = await _storageMethods.uploadImage(
            type: fileType,
            fileName: fileName,
            senderProfile: _currentUser.profilePhoto,
            file: selectedFile,
            receiverId: widget.receiverId,
            senderId: _currentUser.firebaseId,
            imageUploadProvider: _imageUploadProvider,
            sender: _currentUser.firebaseId,
            numberOfMessage: ++messageCount,
          );
    
    Login or Signup to reply.
  2. After uploading a file, you can get a URL to download the file by calling the getDownloadUrl() method on the Reference:

    await mountainsRef.getDownloadURL();
    

    For updated documentation please check
    https://firebase.google.com/docs/storage/flutter/upload-files

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search