can I CRUD a folder to the Supabase bucket from flutter app using supabase_flutter package?
for example create folder inside 'my_bucket' bucket on Supabase storage and upload a file to it using the code bellow:
final bytes = File('path').readAsBytesSync();
String link = '';
final fileExt = path.split('.').last;
final supabase = Supabase.instance.client;
try {
await supabase.storage
.from('my_bucket') // create folder from here
.uploadBinary(
'file.$fileExt',
bytes,
);
link = await supabase.storage
.from('my_bucket') // get alink for the file
.createSignedUrl('file.$fileExt');
} on PostgrestException catch (e) {
print('ErrorSupa:${e.message}');
}
You can just pass the path to the file like the following, and necessary folders will be created automatically.
final bytes = File('path').readAsBytesSync();
String link = '';
final fileExt = path.split('.').last;
final supabase = Supabase.instance.client;
await supabase.storage
.from('my_bucket') // create folder from here
.uploadBinary(
'path/to/file/file.$fileExt',
bytes,
);
link = await supabase.storage
.from('my_bucket') // get alink for the file
.createSignedUrl('path/to/file/file.$fileExt');
I would like to create an app folder to store app related data into and the folder should be located "On my iPhone".
Ive tried the following code:
Future<String> createFolder(String cow) async {
final dir = Directory((Platform.isAndroid
? await path_provider.getExternalStorageDirectory() //FOR ANDROID
: await path_provider.getApplicationSupportDirectory() //FOR IOS
)!
.path + '/$cow');
var status = await Permission.storage.status;
if (!status.isGranted) {
await Permission.storage.request();
}
if ((await dir.exists())) {
return dir.path;
} else {
dir.create();
return dir.path;
}
}
When i open "Files" on an ios Simulator from the homescreen there is no folder created,
iam not sure which location is represented by "getApplicationSupportDirectory".
I found this code on this articel How To Create Folder in Local Storage/External Flutter?
What iam doing wrong?
thank you
This is the code to the function which i used for creataing a folder.
note: i have tested if the folder exist using the .exist function and its already exist, but i can not find it in the file manager app.
Future<String> getFilePath() async {
Directory endPointDirectory=Directory("");
const String folderName="cameraFlutter";// there folder where we will save files to it
var directory=getExternalStorageDirectory()
.then((value) async {
final Directory endPointDirectory=Directory("${value!.path}/$folderName");
if (await endPointDirectory.exists()) {
print("exist");
print(endPointDirectory.path);
} else {
//if folder not exists create folder and then return its path
print("not exist");
await endPointDirectory.create();
}
}).catchError((onError){
print(onError.toString());
});
return endPointDirectory.path;//external storage directory
}
enter image description here
just print to see the path
...
....
final Directory endPointDirectory = Directory("${value!.path}/$folderName");
print(endPointDirectory);
....
...
I want exactly this page in flutter
I'm not sure where that screen is coming from. But you can try the file_picker_writable plugin. (shameless plug).
Future<void> _openFilePickerForCreate() async {
final rand = Random().nextInt(10000000);
final fileInfo = await FilePickerWritable().openFileForCreate(
fileName: 'newfile.$rand.codeux',
writer: (file) async {
final content = 'File created at ${DateTime.now()}\n\n';
await file.writeAsString(content);
},
);
if (fileInfo == null) {
_logger.info('User canceled.');
return;
}
final data = await _appDataBloc.store.load();
await _appDataBloc.store
.save(data.copyWith(files: data.files + [fileInfo]));
}
}
this will open a folder chooser dialog for the user and a default file name (given by fileName).
This code example will open the following dialog:
I am working on a Flutter project to syntehsise an string to an audio file. For this reason, I have added flutter_tts as a dependency and implemented the following method with different approaches in order to check the existence of the generated file:
/// Synthesises the current audio cue into an audio file
static Future<void> synthesiseStringToAudioFile() async {
Future<String> finalPath;
Future<File> finalFile;
Uri uriToFile;
String absolutePath;
bool existsPath;
bool existsManually;
bool exists3;
await flutterTts
.synthesizeToFile("This is my first audio synthesizer in Flutter",
audioFileName)
.then((value) => {
// File has been successfully created
if (value == 1)
{
// Gets the path to the generated audio file
finalPath = pathToFile,
finalPath.then((path) async => {
print('AFile :Path to audio file: $path'),
// Check if exists
existsPath = FileSystemEntity.typeSync(path) != FileSystemEntityType.notFound,
print("AFile : Exists? $existsPath"),
existsManually = await File('/storage/emulated/0/Android/data/mypath/files/temp_audio_cue.wav').exists(), // Requieres async function
print("AFile : Exists2? $existsManually"), // RETURNS TRUE
exists3 = await File(path).exists(),
print("AFile : Exists3? $exists3")
}),
// Gets the generated file
finalFile = localFile,
finalFile.then((file) => {
// Absolute path
absolutePath = file.absolute.path,
print('AFile : AbsolutePath: $absolutePath'),
// Check the URI
uriToFile = file.uri,
print('AFile : URI to audio file: $uriToFile'),
}),
}
else
{print('There was an error during the synthezisation')}
});
}
static void setAudioFileName() {
audioFileName = Platform.isAndroid ? "temp_audio_cue.wav" : "temp_audio_cue.caf";
}
/// Gets the path to the file to be accessed
static Future<String> get pathToFile async {
final path = await localPath;
return '$path/$audioFileName';
}
/// Gets the path to the local directory
static Future<String> get localPath async {
final dir = await getApplicationDocumentsDirectory();
return dir.path;
}
Once the synthesisation is completed, flutterTts.synthesizeToFile() logs in console the following message:
D/TTS (10335): Successfully created file :
/storage/emulated/0/Android/data/mypath/files/temp_audio_cue.wav
so if I check the existence of the file manually (as I do with existManually) will get a true value, but I am not able to do it trying to get dynamically the path as in the other examples I am trying but the ones I am getting are:
/data/user/0/mypath/app_flutter/temp_audio_cue.wav
so it is missing the beginning
/storage/emulated/0/Android/
I was wondering what is the correct way to get the path to the file (missing)?
With path_provider in Android save in getExternalStorageDirectory and in iOS save in getApplicationDocumentsDirectory..
If you want to get this path : /storage/emulated/0
Use path_provider_ex package, which provides root and app files directory for both "external storage" (internal flash) and SD card (if present), as well as available space for each storage.
you can use path_provider package of flutter
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
https://pub.dev/packages/path_provider
I am using filesystem_picker to return absolute paths from storage and then using File('path_string') or Directory('path_string') to get the actual file.
Using manageExternalStorage permissions allows this work, but keep in mind:
"The Google Play store has a policy that limits usage of MANAGE_EXTERNAL_STORAGE".
This also may not work depending on the SDK you are using and/or conflicts from other packages.
import 'package:filesystem_picker/filesystem_picker.dart';
import 'package:permission_handler/permission_handler.dart';
Directory? rootDir;
late String tempDir;
_getFile(){
await _pickFile();
String path = tempDir;
var file = File(path);
// Do stuff with file
}
// Call this before _pickFile(), ideally inside initState()
Future<void> _prepareStorage() async {
rootDir = Directory('/storage/emulated/0/');
var storageExternal = await Permission.manageExternalStorage.status;
if (storageExternal != PermissionStatus.granted) {
await Permission.manageExternalStorage.request();
}
bool b = storageExternal == PermissionStatus.granted;
//mPrint("STORAGE ACCESS IS : $b");
}
Future<void> _pickFile(BuildContext context) async {
await FilesystemPicker.open(
title: 'Select file',
context: context,
rootDirectory: rootDir!,
fsType: FilesystemType.file,
pickText: 'Select file to add',
folderIconColor: Colors.teal,
requestPermission: () async => await Permission.manageExternalStorage.request().isGranted,
).then((value){
if (value != null){
tempDir = value.toString();
}
else{
tempDir = "";
}
//mPrint(tempDir);
});
}
Add the following to AndroidManifest.xml:
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" android:minSdkVersion="30" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:label="test_flutter"
android:name="${applicationName}"
android:icon="#mipmap/ic_launcher"
android:requestLegacyExternalStorage="true"
>
I tried and it worked
import 'package:path_provider/path_provider.dart';
Future<File> getImageFileFromAssets(Asset asset) async {
final byteData = await asset.getByteData();
final tempFile =
File("${(await getTemporaryDirectory()).path}/${asset.name}");
final file = await tempFile.writeAsBytes(
byteData.buffer
.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes),
);
return file;
}