I tried to upload the video using image picker to firebase storage and get the link but there's a problem!
At first, this is my upload video method:
Future<void> uploadVideo() async {
await ImagePicker.pickVideo(source: ImageSource.gallery).then((value) {
setState(() {
_video = value;
});
});
StorageReference storageReference = FirebaseStorage.instance
.ref()
.child('revisionVideos/${Path.basename(_video.path)}');
StorageUploadTask storageUploadTask = storageReference.putFile(
_video, StorageMetadata(contentType: 'video/mp4'));
await storageUploadTask.onComplete;
print('Video Uploaded');
storageReference.getDownloadURL().then((value) {
setState(() {
_videoUrl = value;
});
});
}
When I upload the video, there was a photo (there's an error when trying to preview it) not a video.
And in the run tab:
W/StorageUtil(17625): no auth token for request
W/NetworkRequest(17625): no auth token for request
W/StorageUtil(17625): no auth token for request
W/NetworkRequest(17625): no auth token for request
D/UploadTask(17625): Increasing chunk size to 1048576
Although I edited the rules in the storage and cloud firestore, too.
Add: I didn't add authentication page for this application and the firebase project is linked to 2 android apps.
Related
I am building a mock social media app that allows users upload profile pictures to create their profiles. I am building this app in flutter.
Right now, here's my uploadPic method:
Future uploadPic(BuildContext context) async{
String fileName = basename(_image.path);
FirebaseStorage storage = FirebaseStorage.instance;
var photoUrl;
Reference ref = storage.ref().child(fileName);
var storedImage = File(_image.path);
UploadTask uploadTask = ref.putFile(storedImage);
uploadTask.then((res) {
photoUrl = res.ref.getDownloadURL();
});
print("photo url -- $photoUrl");
await widget.user?.updatePhotoURL(photoUrl);
await FirebaseAuth.instance.currentUser?.updatePhotoURL(photoUrl);
}
I have added the right permissions in my firebase console to allow read and write to the database. I know this because when I check firebase storage, I see the pictures have been uploaded successfully. However, when I try to print the returned photo url (after the upload task completes), I get a null value. This is a problem when I try to access the photo in other areas in the app, because the user photoUrl in firebase is null.
Is there anything I am doing wrong concerning the upload of the pic to firebase?
Also, my terminal returns this: No App Check token for request.. I'm wondering if this has anything to do with the issue?
Any help would be greatly appreciated!
your problem is simple, it's exactly from here:
var photoUrl;
uploadTask.then((res) {
photoUrl = res.ref.getDownloadURL();
});
print(photoUrl); // null
the problem is you are trying to print photoUrl variable without waiting for the future uploadTask to finish its work which results null.
the solution is to wait for uploadTask future before printing the photoUrl variable:
var photoUrl;
await uploadTask.then((res) async {
photoUrl = await res.ref.getDownloadURL();
});
print(photoUrl);
the solution on your code:
Future uploadPic(BuildContext context) async{
String fileName = basename(_image.path);
FirebaseStorage storage = FirebaseStorage.instance;
var photoUrl;
Reference ref = storage.ref().child(fileName);
var storedImage = File(_image.path);
UploadTask uploadTask = ref.putFile(storedImage);
await uploadTask.then((res) async {
photoUrl = await res.ref.getDownloadURL();
});
print("photo url -- $photoUrl");
await widget.user?.updatePhotoURL(photoUrl);
await FirebaseAuth.instance.currentUser?.updatePhotoURL(photoUrl);
}
Is it possible to upload pdf in firebase collection in Flutter.
How can download pdf from firebase
Firebase Realtime Database or Firebase Cloud Firestore can not host any file. But Firebase has a product called Cloud Storage that can host the files and actually keep reference to it in your Cloud Firestore.
What you need to do is to first upload the file and then get the URL of it and keep it in your Cloud Firestore:
Future<String> uploadFile(String filePath) async {
File file = File(filePath);
try {
await firebase_storage.FirebaseStorage.instance
.ref('uploads/bills.pdf')
.putFile(file);
String downloadURL = await firebase_storage.FirebaseStorage.instance
.ref('uploads/bills.pdf')
.getDownloadURL();
return downloadUrl;
} on firebase_core.FirebaseException catch (e) {
print(e);
}
}
How to show the image downloaded from the firebase storage always in a flutter application statically, can be opened and viewed on a click.
I have already uploaded an image from camera and gallery to firebase storage. Now I want to download the image from the firebase storage and make a provision that the images downloaded will be readily stay in the application without disappearing.
I mean when ever user visits that flutter application screen, the image or images downloaded should be shown.
Uploaded the image using using firebase storage:
final FirebaseStorage _storage =
FirebaseStorage(storageBucket: 'gs://flutter-imageupload-cb66a.appspot.com');
StorageUploadTask _uploadTask;
_startUpload() async {
String imagePath = 'DeviceImages/DeviceImage${DateTime.now()}.png';
final StorageReference storageReference = FirebaseStorage().ref().child(imagePath);
final StorageUploadTask uploadTask = storageReference.putFile(imageFile1);
await uploadTask.onComplete;
_addPathToDatabase(imagePath);
print("Image uploaded");
setState(() {
_uploadTask =
_storage.ref().child(imagePath).putFile(imageFile1);
});
}
used firebase_storage: ^3.1.6 and firebase_database:
used image_picker: ^0.6.7+17 for picking the image from camera and Gallery.
To download an image from firebase storage, try the following:
final StorageReference storageReference = FirebaseStorage().ref().child(imagePath);
final UploadTask uploadTask = storageReference.putFile(imageFile1);
uploadTask.then((res) {
res.ref.getDownloadURL();
});
UploadTask extends the Future class therefore you can use then() which registers a callback which will be called after the future returns a result. Then you will be able to get the url using the method getDownloadUrl().
Once you have the url you can use CachedNetworkImage class and do the following:
CachedNetworkImage(
imageUrl: imgUrl,
);
You need to download the package first:
https://pub.dev/packages/cached_network_image
Check the guide here on how to display images from the internet:
https://flutter.dev/docs/cookbook/images/network-image
https://flutter.dev/docs/cookbook/images/cached-images
I'm currently working on uploading multiple images to firebase and getting the downloadUrl into a list.
_imageList.forEach((element) async {
final StorageReference _ref = FirebaseStorage.instance
.ref()
.child("Users/ads/");
StorageUploadTask uploadTask =
_ref.putFile(element as i.File);
await uploadTask.onComplete;
print("Images uploaded");
String image_links = await _ref.getDownloadURL();
_imageUrls.add(image_links);// _imageUrls is a List
});
and now I wanna assign the _imageUrls into firestore.
Map<String, dynamic> ads = {
'adTitle': widget.title,
'adPrice': widget.price,
'adCategory': widget.dropdownValue,
'adCondition': this.newCondtionVal,
'adDesc': widget.desc,
'user_id': uID,
'imageUrls_List': _imageUrls,//Images goes hear
};
crudObj
.addData(ads)
.then((value) => {
showInSnackBar("dataAdded"),
})
.catchError((e) {
print(e);
});
But the problem is:uploading data to firestore works first(Image upload is not finished yet), So now imageUrls_List slot in firestore is empty.
Is there any way to add onComplete to the getDownloadURL() function?
or any other options to run the firestore upload after the images done with uploading?
I have a problem. Everytime when I search a image from my gallery for my profilepicture its work. even when I pressed the log out button and log in again there is still my profilpicture. But when I closed the app and I restart the app then the profilepicture is gone. Does anyone know how to save the URL correctly?
Future uploadPic(BuildContext context) async {
String fileName = basename(_image.path);
StorageReference firebaseStorageRef = FirebaseStorage.instance.ref().child(
fileName);
StorageUploadTask uploadTask = firebaseStorageRef.putFile(_image);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
var downUrl = await (await uploadTask.onComplete).ref.getDownloadURL();
Constants.URL_Profil_Picture = downUrl.toString();
setState(() {
print('Profile Picture uploaded');
Scaffold.of(context).showSnackBar(
SnackBar(content: Text('Profile Picture Uploaded')));
});
}
image: (_image != null) ? FileImage(
_image)
: NetworkImage(Constants.URL_Profil_Picture),
This seems the cause of the problem:
Constants.URL_Profil_Picture = downUrl.toString();
My guess it that when you restart the app, the Constants.URL_Profil_Picture is set to its hard-coded value again.
You'll need to store the URL somewhere that survives application restarts. The common approaches for that are:
Store the URL in shared preferences, or some other form of local storage. This makes the URL available only on the device where it was uploaded from.
Store the URL in a cloud database, such as Cloud Firestore or Realtime Database. This is the most common option, as it gives you the most flexibility.
Given that you're talking about user profiles: if you're using Firebase Authentication to sign in the user, and this image is their profile picture, you can also store it in the user profile.