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
you can go firebase consolse, in firestore databse, config the rule, i guess that
Modified firestore rule for your setup:
Try with this i hope it’s useful
HAPPY CODING 🙂