How do I update a field in a collection with image url? I can update with other strings but the image url doesn't seem to work. I first upload this image imageUrl in a different subcollection called "Documents". It works. I want to use the same imageUrl to UPDATE it's parent collection called 'users' with the field 'Document'. but it doesn't work. Any help?
File? image;
String? imageUrl = "";
String uid = FirebaseAuth.instance.currentUser!.uid;
Future<File> customCompressed(
{required File imagePathToCompress,
quality = 100,
percentage = 10}) async {
var path = await FlutterNativeImage.compressImage(
imagePathToCompress.absolute.path,
quality: 100,
percentage: 80);
return path;
}
Future<File?> pickImages() async {
File? image;
try {
final pickedImage =
await ImagePicker().pickImage(source: ImageSource.camera);
if (pickedImage != null) {
image = File(pickedImage.path);
File compressedImage =
await customCompressed(imagePathToCompress: image);
setState(() {
image = compressedImage;
});
}
} catch (e) {
showSnackBar(context, e.toString());
}
return image;
}
upload() async {
final authProvider = Provider.of<AuthProvider>(context, listen: false);
String uid = FirebaseAuth.instance.currentUser!.uid;
var imageFile = File(image!.path);
FirebaseStorage storage = FirebaseStorage.instance;
Reference ref = storage.ref().child("Document").child(uid);
UploadTask uploadTask = ref.putFile(imageFile);
await uploadTask.whenComplete(() async {
var url = await ref.getDownloadURL();
imageUrl = url.toString();
}).catchError((onError) {
return onError;
});
Map<String, dynamic> getdata = {
"document": imageUrl,
"Full name": authProvider.userModel.fullName,
"Email": authProvider.userModel.email,
};
CollectionReference collectionReference = FirebaseFirestore.instance
.collection('users')
.doc(uid)
.collection('Documants');
collectionReference.add(getdata);
}
// for selecting image
void selectImage() async {
image = await pickImages();
}
CollectionReference ref = FirebaseFirestore.instance.collection('users');
TextButton(onPressed: () { upload();
ref.doc(uid).update({'Status': 'Pending verification'});
ref.doc(uid).update({'Document': imageUrl});
},
child: const Text('Upload document',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),))