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
Related
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
I had initially run flutter pubs get images an it'd get all images folders/files, but, on adding another image folder/files, The files are no longer getting it assets folder files except the previous ones which are still active.
I had all the indentation set correctly, which the previous folders/files were gotten correctly, on adding 3 extra images I got these errors:
Contains emphasized items,
Untracked.
\ uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/
- images/img-icons/
- images/side-bar/
- images/dashboard/
- images/meditation/
The images that are giving issues, they all have a .png extensions:
Note: I had ran flutter pub get more than 10 times!!!
What am I doing wrong?
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.
Is there a way to programatically access the assets folder and loop over all its content?
I want to display all my assets for something like an emoji picker.
I tried searching the web for a solution but all the answers was for a single file from assets.
It was a restriction in the early stage of flutter i guess. But now you can add/read all assets.
In the pubspec.yaml file include the whole folder with /
flutter:
assets:
- assets/
official docs
Programatically you can acces, you should pass/use the asset's name dynamically in that case. There are lots of packages are available for emoji, I think that will be better to use one of them.
For accessing single file we can do by file include pubspec.yaml folder with /:
flutter:
assets:
- assets/res/my_file.txt
And For access all assets from asset folder :
flutter:
assets:
- assets/
Try this, Hope you get the answer!
the flutter compiler lists all files from these folders in the AssetManifest.json file. All we have to do is read this file:
final manifestJson = await DefaultAssetBundle.of(context).loadString('AssetManifest.json');
final images = json.decode(manifestJson).keys.where((String key) => key.startsWith('assets/images'));
When developing for iphone, in Xcode/Swift there is an asset file where you add all the icons your application will use with 1, 2 and 3x sizes. There is probably a similar thing with Android that I don't know yet.
When developing for Flutter:
How do you deal with the different screen resolutions to use the right icon sizes?
If similar to Xcode/Swift, where do you put those files and how do you get them? What about Android?
If you know a tutorial or web page, just let me know! The only ones that I found are only dedicated for app icon and launch screen.
Thanks
Just check the official documentation about resolution-aware-image.
This is based on the device pixel ratio.
Here you have an example about the structure of your files.
.../pubspec.yaml
.../icons/heart.png
.../icons/1.5x/heart.png
.../icons/2.0x/heart.png
And this is how you have to declare the assets files into your pubspec.file
flutter:
assets:
- icons/heart.png
This is the link: https://flutter.io/docs/development/ui/assets-and-images#declaring-resolution-aware-image-assets
To add to #diegoveloper answer.
Some plugins like google-maps do not honour this standard and require you to call BitmapDescriptor.fromAsset('icons/heart.png') which is not converted to the correct image. In this case you can get the correct image from AssetImage by first createLocalImageConfiguration using the BuildContext as defined here. Then use this configuration to resolve the correct image as follows:
ImageConfiguration config = createLocalImageConfiguration(context);
AssetImage('icons/heart.png')
.obtainKey(config)
.then((resolvedImage) {
print('Name: ' + resolvedImage.onValue.name);
});
In summary
Create an asset folder with the naming convention:
pathtoimages/image.png
pathtoimages/Mx/image.png
pathtoimages/Nx/image.png
pathtoimages/etc.
Where M and N are resolutions (2.0x) or themes (dark).
Then add the image or all the images to the pubspec.file as either
flutter:
assets:
- pathtoimages/image.png
or
flutter:
assets:
- pathtoimages/
Load the images in using either AssetImage('pathtoimages/image.png') or resolving to the actual file name as mentioned above, depending on the library being used.