Home > Software engineering >  flutter firebase storage throwing unknown error
flutter firebase storage throwing unknown error

Time:07-22

edit: i notice that the code only runs till the below code and then gets out with the error. also, do note that the code provided used to work today

void pickUploadGalleryImage() async {
    final image = await ImagePicker().pickImage(
      source: ImageSource.gallery,
      maxWidth: 512,
      maxHeight: 512,
      imageQuality: 100,
    );

    Reference ref = FirebaseStorage.instance
        .ref()
        .child("member_profile_pictures/${fileName getRandomString(50)}");

i am trying to upload a profile picture to firebase storage and then display it on a circular avatar. my code is as follows.

void pickUploadCameraImage() async {
    final image = await ImagePicker().pickImage(
      source: ImageSource.camera,
      maxWidth: 512,
      maxHeight: 512,
      imageQuality: 100,
    );

    Reference ref = FirebaseStorage.instance
        .ref()
        .child("member_profile_pictures/${fileName   getRandomString(50)}");

    await ref.putFile(File(image!.path));

    ref.getDownloadURL().then((value) {
      print(value);
      setState(() {
        imageUrl = value;
      });
    });
  }

  void pickUploadGalleryImage() async {
    final image = await ImagePicker().pickImage(
      source: ImageSource.gallery,
      maxWidth: 512,
      maxHeight: 512,
      imageQuality: 100,
    );

    Reference ref = FirebaseStorage.instance
        .ref()
        .child("member_profile_pictures/${fileName getRandomString(50)}");

    await ref.putFile(File(image!.path));

    ref.getDownloadURL().then((value) {
      print(value);
      setState(() {
        imageUrl = value;
      });
    });
  }

however, the terminal is throwing me with this error.

[VERBOSE-2:ui_dart_state.cc(198)] Unhandled Exception: [firebase_storage/unknown] An unknown error occurred, please check the server response.

i have tried to deleting the firebase storage bucket and recreating a new one but it is still giving me this error. tia for all inputs

CodePudding user response:

Try this way.

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

final int timestamp = DateTime.now().millisecondsSinceEpoch;
final ImagePicker picker = ImagePicker();
final XFile? image = await picker.pickVideo(source:ImageSource.gallery);
final prefixStorage.Reference firebaseStorageRef = firebaseStorage
            .ref()
            .child('images')
            .child(
                '/$timestamp${image?.path.substring(image.path.indexOf('.'), image.path.length)}');
await firebaseStorageRef.putData(await image.readAsBytes());
final String? imageURL = await firebaseStorageRef.getDownloadURL();

CodePudding user response:

Try giving it a child when you are uploading image to 'member_profile_pictures'.

void pickUploadCameraImage() async {
        final image = await ImagePicker().pickImage(
          source: ImageSource.camera,
          maxWidth: 512,
          maxHeight: 512,
          imageQuality: 100,
        );
    
        Reference ref = FirebaseStorage.instance
            .ref()
            .child("member_profile_pictures").child('${fileName   getRandomString(50)}');
    
        await ref.putFile(File(image!.path));
    
        ref.getDownloadURL().then((value) {
          print(value);
          setState(() {
            imageUrl = value;
          });
        });
      }
    
      void pickUploadGalleryImage() async {
        final image = await ImagePicker().pickImage(
          source: ImageSource.gallery,
          maxWidth: 512,
          maxHeight: 512,
          imageQuality: 100,
        );
    
        Reference ref = FirebaseStorage.instance
            .ref()
            .child("member_profile_pictures").child('${fileName   getRandomString(50)}');

    
        await ref.putFile(File(image!.path));
    
        ref.getDownloadURL().then((value) {
          print(value);
          setState(() {
            imageUrl = value;
          });
        });
      }

CodePudding user response:

update: turns out, i have to use a vpn where my storage is located to access the file. everything works as it should be now

  • Related