Flutter Web - File doesn't update when edited externally - flutter

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

Related

Display an image from the asset in the documentation

I'm writing documentation about a variable and I would like to include an image that is inside the project. For example:
assets/
|- icons/
| |- my-image.svg
lib/
|- my_file.dart
I know it is possible to display an image from an URL, but what about one from a file?
Those were my unsuccessful tries:
// lib/my_file.dart
/// The image attempt:
/// ![](assets/icons/my-image.svg)
/// ![](../assets/icons/my-image.svg)
const myVariable = 0;
But it doesn't work:
Is there a way to do it?
There is already an issue on GitHub: #2390
and also a related thread: Using local image assets in dart documentation comments
Effectively, web urls are working, but relative paths aren't, because VSCode doesn't support that. It tries to open the files relative to its application directory. But I can't confirm that for Windows either, because for me, not even absolute paths are working...
So if your project lives in a public repository anyways, you could just use a web url.
1:open pubspec.yaml and click "Pub get"
2:
Image.asset("assets/icons/my-image.svg",width: 100,height: 100,)
So i think you want to let the variable hold the asset image path so do this
Go to pubspecs.yaml file and add this
flutter:
assets:
- assets/icons/my-image.svg
Then run flutter pub get
then you can use the image this way
//let the variable hold the image path
const myVariable = "assets/icons/my-image.svg";
//to display on widget
Image.asset(myVariable);

Flutter: VideoPlayerController.asset does not play specified video

I am trying to play a video. I have added the necessary permissions.
I am using Android so I added this after :
<uses-permission android:name="android.permission.INTERNET"/>
Pubspec:
assets:
- assets/
Code initializing controller:
_controller = VideoPlayerController.asset("assets/video_1.mp4");
When I try with network urls (videos from the internet), it works just fine. I think something is wrong with specifying the video I want.
Where I put my video
What I see on emulator
This is the full code: https://pastebin.com/32BVzBmM. It's copied from the Flutter VideoPlayer documentation example, the only thing changed being the one line above.
It looks like your assets import is incorrect, make sure you have 2 spaces difference between the "assets:" and asset name like this:
assets:
- assets/ // <- this should work
- assets/ // <- this will not work
Don't forget to kill the app, run flutter clean && flutter pub get before running the app

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 crashing on failed PlatformAssetBundle

Running my app I get a crash, but stack trace doesn't go deep enough because of an Async call? I cannot figure out what is trying to grab an image?
Below is the assets part of my pubspec.yaml file
assets:
- assets/
- assets/images/
Using VSCode and it shows any images I reference or if I mouse over the reference it shows the images.
#protected
Future<ui.Codec> _loadAsync(AssetBundleImageKey key, DecoderCallback decode) async {
ByteData data;
// Hot reload/restart could change whether an asset bundle or key in a
// bundle are available, or if it is a network backed bundle.
try {
data = await key.bundle.load(key.name);
} on FlutterError {
PaintingBinding.instance.imageCache.evict(key);
rethrow; <---- crashes here
}
I am not grabbing any external images (URL) in my code. I added Google Maps SDK so maybe its something it is trying to grab? But not sure why no stack trace?
VSCode see the images before I send it to the iOS simulator. These are the images that it cannot find. It finds all other images referenced in the file.
Below is the assets part of my pubspec.yaml file
assets:
- assets/
- assets/images/
Try to follow the indentation of the documentation.
Here is an example:
flutter:
assets:
- assets/my_icon.png
- assets/background.png
If the error still exist, try to follow the workaround here:
It worked for me:
Set only full path to asset in pubspec.yaml
e.g.
assets:
- assets/files/asset.txt
- assets/images/icons/myicon.png
Whole folder not worked for me - assets/
Add ./ to the beginning of your asset path when you loading it
e.g. rootBundle.loadString('./assets/path/to your/asset.txt
flutter clean
Also, as mentioned in this comment, using flutter clean is a good idea if there are some weird behavior from your app.