Can't remove PDF file from cache Flutter - 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

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)

How to launch system File Manager from Flutter app?

From my flutter I want to launch the default File Manager. I don't want to select file from it for my app. I just want to launch it (preferably showing the directory that I want it to show) so that the user can pick a file from that directory and do whatever they want with it.
Can anyone please tell me how to do it? I see a lot of code and examples for picking a file but that's not what I need. Just want to launch whatever is a default file manager.
Please do not suggest FilePicker. That is not what I am looking for. I do not want to "pick" a file. I know how to pick a file. I just want the default file manager app to be launched. I don't want the control to be returned back to the app irrespective of whether the user selects a file or not.
This was causing me grief too. Here's my solution.
Using 'external_app_launcher' you are able to launch apps based on their URL scheme, so we have...
import 'package:external_app_launcher/external_app_launcher.dart';
okButton = TextButton(
child: const Text("Open Files"),
onPressed: () async {
await LaunchApp.openApp(iosUrlScheme: 'shareddocuments://', openStore: false);
},
);
This will launch the app, I'm still working my solution as I want to open my App's folder so will update as I get more.
You can use the file_picker package.
Usage
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result != null) {
File file = File(result.files.single.path);
} else {
// User canceled the picker
}
Edit: Now I understand what you want. Check https://pub.dev/packages/external_app_launcher

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...
}

Flutter - issue with downloading the file and opening it with the default app on ios

I have built an app with flutter, what I am trying to do is download a document, these are jpeg, jpg or pdf files, from my REST api, and open it within the app so the user can see it,
The below piece of code is the issue, I am trying to use the openfile package in flutter, the code works perfectly on android, it opens the image automatically, but on ios, it gives the attached screen after attempting to download.
I am putting part of my code which I believe is the issue.
Code to specify the plugin parameter
if (Platform.isIOS)
directory = await getTemporaryDirectory();
else
directory = await getExternalStorageDirectory();
Code to open the file
OpenFile.open(downloadPath).then((_result) {
_closePopup();
print(_result);
}).catchError((error) {
print("Could not open file *** $error");
FileDownloader.lastError =
"Could not open file: " + error.toString();
_closePopup();
});
} else {
Any guidance on this will help.
Buttton sample I am trying to work out
IOS Behaviour with code