Uploading audio to firebase flutter times out - flutter

I am trying to upload an audio file to firebase within my flutter app.
The user flow is filePicker -> confirm screen (where the user picks the details of the song like name, caption, etc). If I wait too long in the confirm screen (longer than the length of the audio file), the file "times out" and returns false for file.existsSync().
However, whenever I run through the confirm screen super fast, it works because I uploaded it before the length of the audio file finishes. I assume this has to do with caching, but I have no idea how to fix this.
My code for uploading is below:
Future<String> _uploadSongToStorage(String id, String songPath) async {
Reference ref = firebaseStorage.ref().child('songs').child(id);
//assert(songFile.existsSync());
UploadTask uploadTask = ref.putFile(File(songPath));
TaskSnapshot snap = await uploadTask;
String downloadUrl = await snap.ref.getDownloadURL();
return downloadUrl;}

Related

upload and get image from firebase storage flutter web

I am currently developing a web filter application
In the application, when I enter product details, I need to choose an image of the product
But I face a problem, when I choose an image from the computer, I cannot get the image path, the name and the bytes only, and when I upload the bytes to the firebase, I cannot get the image because it is bytes. How can I calculate the path of the image and display it in a list?I am currently developing a flutter web application
In the application, when I enter product details, I need to choose an image for the product from my PC
But I face a problem, when I choose an image from the computer, I cannot get the image path, the (i get name and the bytes only), and when I upload the bytes to the firebase, I cannot get the image because it is bytes not as path. How can I get the path of the image and display it in a list products?
Uint8List? fileBytes;
String? fileName;
String? picUrl;
void chooseImage() async {
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result != null) {
fileBytes = result.files.first.bytes;
fileName = result.files.first.name;
}
}
uploadImageToFirebase() async {
UploadTask _uploadTask = (await FirebaseStorage.instance
.ref('uploads/$fileName')
.putData(fileBytes!)) as UploadTask;
picUrl = await (await _uploadTask).ref.getDownloadURL();
}

after using file.copy(path) and getting the new file i get the previos file at that location

I'm trying to update a locally saved image file (user's profile picture).
This is how I do it:
Future<void> setProfilePic(File newProfilePic) async {
String profilePicPath = (await getApplicationDocumentsDirectory()).path +
Platform.pathSeparator +
"profilePic.jpg";
await newProfilePic.copy(profilePicPath); }
Then I try to get the profile picture like so:
Future<File> getCurrentUserProfilePic() async {
String filePath =
(await getApplicationDocumentsDirectory()).path + "/profilePic.jpg";
File profilePicFile = File(filePath);
return profilePicFile; }
The profile picture I get is the previous picture located in that file.
Only after I reopen the app, the widget shows the new profile picture.
I do use await when calling the get function and I do setState().
Does anyone know what the problem is?

How do I get a public link for my video stored in firebase

I am using a service called mux in which it takes a url link to a video and encodes transcodes and decodes for you. It takes a link to a video. However how do I get this linke in firebase flutter?
Current This is what I am doing. This leads to the video only showing the first frame.
await firebase_storage.FirebaseStorage.instance
.ref().child('Users/$currentSignedInEmail/Video/${titleController.text}')
.putFile(compressedVersion).catchError((error){
});
//TODO: need to fix this does not load properly
_muxClient.storeVideo(videoUrl: await firebase_storage.FirebaseStorage.instance
.ref().child('Users/$currentSignedInEmail/Video/${titleController.text}').getDownloadURL());
Try this:
Reference reference = firebase_storage.FirebaseStorage.instance
.ref()
.child('Users/$currentSignedInEmail/Video/${titleController.text}');
UploadTask task = reference.putFile(compressedVersion);
await task;
var url = await reference.getDownloadURL();
_muxClient.storeVideo(url);

Saving images and videos for offline access

I am developing an app that fetches a custom object from my REST API. The object's class is called MyItem and it looks like this:
class MyItem {
String title;
String profilePicURL;
String videoURL;
}
As you can see, the class contains two URLs, that points to a png and mp4 files.
I would like to implement a feature, which allows the user to download the object, in order to access its content offline. I have no problem saving the title property, but how can I save the two URLs (because I don't want the URL itself to be saved, I would like to save the file it points to).
Any idea what is the best way doing that in Flutter and Dart?
Thank you!
import 'package:dio/dio.dart';
import 'package:path_provider/path_provider.dart';
var directory = await getApplicationDocumentsDirectory();
Dio dio = Dio();
//Below function will download the file you want from url and save it locally
void Download(String title, String downloadurl) async{
try{
await dio.download(downloadurl,"${directory.path}/$title.extensionoffile",
onReceiveProgress: (rec,total){
print("Rec: $rec, Total:$total");
setState(() {
//just to save completion in percentage
String progressString = ((rec/total)*100).toStringAsFixed(0)+"%";
}
);
});
}
catch(e){
//Catch your error here
}
}
Now again wherever you want just use
var directory = await getApplicationDocumentsDirectory();
String filepath = "{directory.path}/filename.fileextension";
Now you can use this Image.file('filepath'); //to display those image
also you can use
video player plugin
where again VideoPlayerController.file('filepath') //to show video but read documention properly
These are just a whole steps or a broader view, you need to use them as a map and build your code.That is have a proper file name and extension saved or correctly fetched or mapped

Uploading network image to firestore

How can I download an image file from network using a given url and upload it to fire store?
I am having hard time downloading image from url and uploading it to fire store.
you could do it for example like this:
upload() async {
//pick image use ImageSource.camera for accessing camera.
File image = await ImagePicker.pickImage(source: ImageSource.gallery);
//alternative with ressource in your app:
var src = // your image src
File image = await Image.network(src);
//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;
}
To get a better understanding of firebase storage and the upload process, and features like showing progress and more I recommend you checking this useful article too: https://medium.com/flutterpub/firebase-cloud-storage-and-flutter-fa2e91663b95