xxxxxxxxxx
uploadImage() async {
final _firebaseStorage = FirebaseStorage.instance;
final _imagePicker = ImagePicker();
PickedFile image;
//Check Permissions
await Permission.photos.request();
var permissionStatus = await Permission.photos.status;
if (permissionStatus.isGranted){
//Select Image
image = await _imagePicker.getImage(source: ImageSource.gallery);
var file = File(image.path);
if (image != null){
//Upload to Firebase
var snapshot = await _firebaseStorage.ref()
.child('images/imageName')
.putFile(file).onComplete;
var downloadUrl = await snapshot.ref.getDownloadURL();
setState(() {
imageUrl = downloadUrl;
});
} else {
print('No Image Path Received');
}
} else {
print('Permission not granted. Try Again with permission access');
}
}
xxxxxxxxxx
FirebaseStorage storage = FirebaseStorage.instance;
Reference ref = storage.ref().child(pathname + DateTime.now().toString());
await ref.putFile(File(_image.path));
String imageUrl = await ref.getDownloadURL();
print(imageUrl);
xxxxxxxxxx
upload() async {
//pick image use ImageSource.camera for accessing camera.
File image = await ImagePicker.pickImage(source: ImageSource.gallery);
//basename() function will give you the filename
String fileName = basename(image.path);
//passing your path with the filename to Firebase Storage Reference
StorageReference reference =
FirebaseHelper.firebaseStorage().child("your_path/$fileName");
//upload the file to Firebase Storage
StorageUploadTask uploadTask = reference.putFile(image);
//Snapshot of the uploading task
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
}
xxxxxxxxxx
Future<void> fetchCategories() async {
try {
isLoading.value = true;
// Fetch the categories
final categories = await _categoryRepository.getAllCategories();
// Update the categories list
allCategories.assignAll(categories);
// Upload images to Firebase Storage
await uploadImagesToFirebase();
// Filter the featured categories
featuredCategories.assignAll(allCategories
.where((category) => category.isFeatured && category.parentId.isEmpty)
.take(8)
.toList());
} catch (e) {
TLoaders.errorSnackbar(title: 'OOPSS', message: e.toString());
} finally {
isLoading.value = false;
}
}
Future<void> uploadImagesToFirebase() async {
try {
for (final category in allCategories) {
if (category.image.isNotEmpty) {
// Get image data from assets
final imageData =
await TFirebaseStorageService.instance.getImageDataFromAssets(category.image);
// Upload image to Firebase Storage
final imageUrl = await TFirebaseStorageService.instance
.uploadImageData('category_images', imageData, '${category.id}.png');
// Update category's imageUrl with Firebase Storage URL
category.imageUrl = imageUrl;
}
}
} catch (e) {
throw 'Error uploading images to Firebase: $e';
}
}