skip to Main Content

I am new to work with firebase. ı am treying to develop an app which tere is profe page for users. to get banner iamge from the user ı wrote the code below.


File? _bannerImage;

Future<void> _pickBannerImage() async {
  final picker = ImagePicker();
  final pickedFile = await picker.pickImage(source: ImageSource.gallery);

  if (pickedFile != null) {
    File imageFile = File(pickedFile.path);
    setState(() {
      _bannerImage = imageFile;
    });

    User? user = FirebaseAuth.instance.currentUser;
    if (user != null) {
      String userId = user.uid;
      String filePath = 'bannerImages/$userId/banner.jpg'; // Adjusted path structure

      try {
        // Upload file to Firebase Storage
        TaskSnapshot uploadTask = await FirebaseStorage.instance
            .ref(filePath)
            .putFile(imageFile);

        // Get download URL
        String downloadURL = await uploadTask.ref.getDownloadURL();

        // Save download URL in Firestore
        await FirebaseFirestore.instance
            .collection('users')
            .doc(userId)
            .update({
          'bannerUrl': downloadURL,
        });

        print('Banner image uploaded and URL saved in Firestore');
      } catch (e) {
        print('Error uploading banner image: $e');
      }
    } else {
      print('No user is signed in');
    }
  }
}

this code suppose to let every user can change their banner but, firebase can’t download the image. Because of that when ı restart the program, the photo doesn’t come.
ı couldn’t solve the error
.
the code above every time catches error in try catch.

what should ı do

2

Answers


  1. you can go firebase consolse, in firestore databse, config the rule, i guess that

    Login or Signup to reply.
  2. Modified firestore rule for your setup:

    rules_version = '2';
    service firebase.storage {
      match /b/{bucket}/o {
        match /uploads/{userId}/{allPaths=**} {
          allow read, write: if request.auth != null && request.auth.uid == userId;
        }
      }
    }
    

    Try with this i hope it’s useful

    HAPPY CODING 🙂

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