How to get url of uploaded media in supabase storage in flutter? - flutter

In flutter for uploading image according to doc is
final avatarFile = File('path/to/file');
final response = await supabase
.storage
.from('avatars')
.upload('public/avatar1.png', avatarFile, fileOptions: FileOptions(
cacheControl: '3600',
upsert: false
));
So, how we can get the downloadUrl of this uploaded media ?

You can use the getPublicUrl method:
final res = supabase
.storage
.from('avatars')
.getPublicUrl('public/avatar1.png');
final publicURL = res.data;
See https://supabase.com/docs/reference/dart/storage-from-getpublicurl

To receive downloadUrls of your uploaded media, you need to create a subscription to the media table.
The following snippet will create a subscription to the media table and log each media item as it is created:
mediaSubscription = supabase
.from('media')
.subscribe((event) {
print(event.doc.data);
});

Related

maui: showing picture from gallery not working

Im grabbing a picture from the gallery like described in the docs. that works, I can pick the pictures in my ios gallery and I am getting a full stream:
string localFilePath = System.IO.Path.Combine(FileSystem.CacheDirectory, photo.FileName);
using Stream sourceStream = await photo.OpenReadAsync();
using FileStream localFileStream = File.OpenWrite(localFilePath);
await sourceStream.CopyToAsync(localFileStream);
MainThread.BeginInvokeOnMainThread(() =>
{
img_profilePic.Source = ImageSource.FromStream(() => localFileStream);
img_profilePic.IsVisible = true;
});
But when I try to set the image to the imagesource of my image from xaml, nothing changes in the UI.
If I change the image to a local image however, it updates the UI. So it must be something with the stream.
Anyone got any idea?

Flutter Dynamic linking not available for web apps?

My Use Case:
Users in my app can create lists of items. I want the users to be able to share a link to one of their lists.
I want individuals who click the shared link to see the list of items in a web browser and not need the app installed.
Create the Dynamic Link
The first thing I need to do is create a dynamic link for a user to share. I struggled with this at first and then looked at this question here: Flutter - How to pass custom arguments in firebase dynamic links for app invite feature?
After looking at that I build my own class like so:
class PackDynamicLinkService {
Future<Uri> createDynamicLink({required String packID}) async {
final DynamicLinkParameters parameters = DynamicLinkParameters(
// This should match firebase but without the username query param
uriPrefix: 'ttps://appName.app/p',
// This can be whatever you want for the uri, https://yourapp.com/groupinvite?username=$userName
link: Uri.parse('https://appName.app/p?pack=$packID'),
androidParameters: const AndroidParameters(
packageName: 'com.test.demo',
minimumVersion: 1,
),
iosParameters: const IOSParameters(
bundleId: 'com.test.demo',
minimumVersion: '1',
appStoreId: '',
),
);
final dynamicLink = await FirebaseDynamicLinks.instance.buildShortLink(
parameters,
shortLinkType: ShortDynamicLinkType.unguessable,
);
return dynamicLink.shortUrl;
}
}
I then called that function in my app and I am able to get a URL that looks like this: https://appName.app/p?pack=lVeSBUHoLX52zPqAiB9A
So far so good. Now I just need to click the link and read the parameter $packID that I passed to it inside my web app. Then I can route them to the correct view and display the list.
Processing Dynamic Link
in my marin.dart I tried to retrieve the params with:
//get link stuff
final PendingDynamicLinkData? initialLink =
await FirebaseDynamicLinks.instance.getInitialLink();
if (initialLink != null) {
final Uri fullLink = initialLink.link;
print(fullLink.queryParameters['pack']);
}
but when I upload to firebase hosting to see if this code works for testing, I get this error:
Uncaught MissingPluginException(No implementation found for method
FirebaseDynamicLinks#getInitialLink on channel
plugins.flutter.io/firebase_dynamic_links)
After doing some more research it seems that firebase_dynamic_links package may not be available for web? So does that mean my original use case is not possible with flutter at this time?
You would want to take the link received and parse it using something like:
var PendingDynamicLinkData? initialLink =
await FirebaseDynamicLinks.instance.getInitialLink();
if (kIsWeb) {
String link = Uri.base;
initialLink = await
FirebaseDynamicLinks.instance.getDynamicLink(Uri.parse(link));
}
if (initialLink != null) {
final Uri fullLink = initialLink.link;
print(fullLink.queryParameters['pack']);
}
Here, we are checking if its a web app and then we can parse the dynamic link and get the info for it. We just check for the Uri that was used in our web app.

Flutter imagepicker.pickvideo return jpg

I'm using this to return the video file but I got .jpg
Future<File> getVideo() async {
var video = await ImagePicker.pickVideo(
source: ImageSource.gallery);
return video;
}
I want to ImagePicker.pickVideo() return video file instead of .jpg file so I can upload this file to firebase, how can I achieve that?
I'm assuming that you're using the package:
https://pub.dev/packages/image_picker
pickVideo() method has been decrecated, and you will need to replace these apis with getVideo()
As explained the repositories' documentation:
https://github.com/flutter/plugins/tree/master/packages/image_picker/image_picker
Write this:
final _picker = ImagePicker();
PickedFile video = await _picker.getVideo(...)
However I would suggest to use this package as an alternative:
https://pub.dev/packages/flutter_document_picker
This package will allow you to select all videos on the device, including those taken from a users' Google Drive or iCloud providers. In this case write this:
FlutterDocumentPickerParams params = FlutterDocumentPickerParams(
allowedUtiTypes: [
'public.video',
'public.mpeg',
'public.mpeg-4-audio',
'com.apple.protected-​mpeg-4-audio'
],
allowedMimeTypes: [
'video/mpeg',
'video/x-flv',
'video/mp4',
'application/x-mpegURL',
'video/quicktime',
'video/x-msvideo',
'video/x-ms-wmv',
'video/ogg',
'video/mp2t',
'video/3gpp'
],
invalidFileNameSymbols: ['/'],
);
return await FlutterDocumentPicker.openDocument(params: params);
You will need to make sure that the Mimes and Uti types for videos on iOS & Android are set correctly.

Flutter update image in storage

I am using ImagePickerWeb and upload an images in Storage. For display this images I am using url. If user change image, how to update in storage? The first need delete previous image and upload again?
Future uploadImg() async {
var timekey = DateTime.now();
fb.StorageReference storageReference =
fb.storage().ref('imgProduct/${timekey.toString()}.jpg');
fb.UploadTaskSnapshot uploadTask = await storageReference
.put(_image1, fb.UploadMetadata(contentType: 'image/jpg'))
.future;
var imageUrl = await uploadTask.ref.getDownloadURL();
url = imageUrl.toString();
print('Image Url' + url);
}
you need to update the database with the new photo url and then delete the old image from firebase storage.

Modified download url - firebase storage

I would need some method on the server side (cloud function) to give me a 'changing' url when an image is updated with same file name. Is that possible?
Background: I use the recent firebase extension to create thumbnails. When thumbnails are generated, I store the download urls on firestore for access in my mobile app
As per difference between getSignedUrl() and getDownloadUrl() I use getSignedUrl().
But this method gives me 'same' url even when the underlying image changes. And because the url is same the Widget (I use flutter) does not re-build and the modified image is not reflected
exports.thumbnailHandler = functions.storage.object().onFinalize(async (object) => {
const contentType = object.contentType;
const filePath = object.name;
const fileBucket = object.bucket;
const fileRef = admin.storage().bucket(fileBucket).file(filePath);
const options = {
action: 'read',
expires: '12-31-2099'
};
fileRef.getSignedUrl(options).then(results => {
var downloadUrl = results[0];
console.log(downloadUrl); //This is same url everytime, even when image changes but with same file name
}
}
Is there an alternative to signedUrl above something equivalent to downloadUrl from flutter sdk?
Update This is the answer: This gives me the downloadUrl as I would from a client https://stackoverflow.com/a/53744579/11749006