Fetch all Folders from root directories in Flutter - flutter

I want to fetch all the folder from internal and external storage same as any other video player app , how to make it done in flutter for both IOS/Android

you can use ext_storage like this,
void getRootPath() async {
var path = await ExtStorage.getExternalStorageDirectory();
print(path); // /storage/emulated/0
}
and can search iterable.

Related

Open PDF In PDF Viewer

I have a PDF in the assets folder: assets/my_resume.pdf.
How can I open it in a PDF viewer? I don't want to open it in a widget. I want it to open outside of my app with whatever PDF viewer is available to this device.
I tried: https://pub.dev/packages/open_file but that doesn't work with assets.
Future<void> launchPdf() async {
}
How can I do this? I mostly care about mobile so not even considering web.
I don't know if this is the best method, but you could load the pdf file data, and save it to, for example, the Document directory on the device, and after that, use the open file package to open the pdf with a native device app.
void openAsset() async {
// load pdf into ram
ByteData pdf = await DefaultAssetBundle.of(context).load('assets/your_pdf.pdf');
// get documents directory
Directory documents = await getApplicationDocumentsDirectory();
// write data to a file in the documents directory
await File(documents.path + '/your_pdf.pdf').writeAsBytes(pdf.buffer.asUint8List());
// call open_pdf function
// ...
}
(Didn't test the code)

Flutter Web - File doesn't update when edited externally

I have a simple Flutter app with a config folder.
Does someone know how to reload a text file during the runtime?
Every time I open the debug or run the app, if I edit the text file externally it won't update, if I restart the app it will update. How can I make the file content update during runtime, automatically?
The following code is set on a timer in order to make it reread the file during a period of time, but the file isn't updating if edited externally.
Future<List<String>> ReadFromFile() async {
final data = await s.rootBundle.loadString('../../config.yml');
final mapData = loadYaml(data);
var test = json.encode(data);
var test2 = json.decode(test);
List<String> rawString = test2.split('\n');
return rawString;
}
Also, this is my pubspec:
dependencies:
flutter:
sdk: flutter
yaml: ^3.1.0
...
flutter:
uses-material-design: true
assets:
- "../../config.yml"
Please note that this is a Flutter Web project and might not work using solutions for smartphones.
Found what I've been searching for.
Just use an HttpRequest like this:
It works for every file!
var path = '../../config.yml';
HttpRequest.getString(path).then((String fileContents) {
print(fileContents.characters);
}).catchError((error) {
print(error.toString());
});
Also the library is dart:html.
Use it instead of dart:io if you have it.
Edit: The new settings in the file, only updates if the new file generated by Flutter (inside the Flutter project folder) is modified, not the original one. But it still works for me. Better than nothing.
Good luck everyone :D

Flutter folder picker

I have a button on screen, when you click button I need to open some folder picker.
I have searched for package, but I didn't found any package that also support Android and iOS.
Does someone knows some package who will resolve this problem, or something I could use to make solution of picking folder on iOS?
EDIT:
I found way to get directory picker functionality,I didn't test on iOS but in documentation says it works on both (Android and iOS).
String path = await FilePicker.platform.getDirectoryPath();
With this line above you get new screen to select your directory and get a path like result from that Future.
This is solved using file_picker package.
Use the package file_picker.
Easy to use:
FilePickerResult? result = await FilePicker.platform.pickFiles();
if(result != null) {
File file = File(result.files.single.path);
} else {
// Do something else...
}

Can't remove PDF file from cache Flutter

I'm using the following library to display a pdf in my application "https://pub.dev/packages/advance_pdf_viewer".
I'm displaying the pdf from file assets like this:
loadDocument() async {
document = await PDFDocument.fromAsset('assets/res/sample.pdf');
setState(() {
_isLoading = false;
});
}
The problem is that I want to replace the file with a new version, but it keeps showing the old one.
What I tried:
1- Changing file name
2- Flutter clean
3- Pub get
4- Deleting the whole file from the project, and still showing ( which means it's somewhere in the cache )
5- Deleting the app
All previous solutions didn't work, and it's driving me crazy.
Thanks

Flutter Web Save Image

I was working on a flutter web app where I need to save an image created using canvas.I tried FileSaver.js library but it wasn't successful for me.
Index.html main.dart
I created simple package image_downloader_web that can easily download image from web.
final WebImageDownloader _webImageDownloader = WebImageDownloader();
const _url = "https://picsum.photos/200";
Future<void> _downloadImage() async {
await _webImageDownloader.downloadImageFromWeb(_url);
}