How to mock asset file in flutter unit test project? - flutter

Let's say I have a file named some_file.json defined in pubspec.yaml as an asset. In my flutter project I load the file with the loadString function.
await rootBundle.loadString("some_file.json");
Then I read values from the file and I need to create Unit Tests. In the Unit Test project I create file with MemoryFileSystem class.
final MemoryFileSystem memoryFileSystem = MemoryFileSystem();
final File file = memoryFileSystem.file("some_file.json");
file.createSync(recursive: true);
expect(file.existsSync(), true);
The code above successfully creates file, but load method is not able to open the file.
Error:
237:7 PlatformAssetBundle.load
Unable to load asset: build_resources/variables.json
Is there any better approach how to mock asset file in Unit Tests project?

Related

Creating and loading AssetBundle manifest in Unity

I am loading some assets from assetBundles that I store online. I want to cash some of them but I need a bundle manifest for That.I am using this to build the bundle.
BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, BuildTarget.Android);
This gives me enter image description here, first file is assetBundle, the secound is manifest for that assetbundle. Based on what I found online I think it should also create a manifest named atlases (for the folder), but it doesnt.
If i try to load one of the assetBundle manifests using this
AssetBundle manifestBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "atlases/atlascity1") //fails with error: "Unable to open archive file: C:/ProjectPath/StreamingAssets/atlases/atlascity1"
//ver. B:
AssetBundle manifestBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "atlases/atlascity1.manifest") //fails with error: "Unable to read header from archive file: C:/ProjectPath/StreamingAssets/atlases/atlascity1.manifest"
What am I missing?

Flutter : Play Audio File From Application Directory ( Storage )

In Stack Overflow or other example, I found that from asset folder and from network I can play audio file easily.
But, There is no clear approach That I can play audio file from Application Storage or mobile device.
My Audio path is : String playebleFilePath = '/Users/noorhossain/Library/Developer/CoreSimulator/Devices/5C9813C4-7773-4013-B086-279DB8FCA64F/data/Containers/Data/Application/F905830C-C698-4FEB-80F1-FB386BE18882/Library/Application Support/VocalDatabaseAudio/word_audio_one/2.mp3'
final assetsAudioPlayer = AssetsAudioPlayer();
assetsAudioPlayer.open(
Audio.file(playebleFilePath),
);
How can I play Audio from this storage path ?
My error log says : plugin Exception, cannot find the file in this server.
Any solution ?
I think the exception is due to the fact that the file you are trying to play is not in the assets folder which will cause the plugin to throw an exception.
i have been using audioplayers plugin and for assets it actually has a prefix to the path that actually points to an "assets" folder, the assets folder should also be correctly referenced in the pubspec.yaml file...
then more importantly since it seems you want to use app directory, consider using something like:await audioPlayer .play(DeviceFileSource("directory/path to your song"));
just check out the getting started of the audioplayers library: [ [1]: https://github.com/bluefireteam/audioplayers/blob/main/getting_started.md][1]

Couldn't Import a dart file in a new dart file (Target of URI doesn't exist)

My flutter project has a separate dart file for each functionality, So, I tried to import a dart file in a new dart file to access the functionality in that file, but I got this error "Target of URI doesn't exist: 'package:filename.dart'. Try creating the file referenced by the URI, or Try using a URI for a file that does exist"
I tried solutions like restarting the Ide, flutter clean command and pub get, Most of the solutions for the same error have to deal with packages, it can be solved by adding package name in dependencies. But this is a dart file, how do I solve it.
When using the import function and going to the root folder with 'package:*', you need to first write the flutter project folder name, and then after that the file or subfolders.
All this but skipping the 'lib' folder.
As an example:
import 'package:flutterProject/subFolderInLib/filename.dart'
Hope this was understandable and worked! :)

Flutter: Package can't load a string resource using rootBundle.loadString - asset indenting correct

I have a project that consumes another package on the drive using a relative path:
project1 (setup as a full flutter project with flutter create project1)
project2 (setup with flutter create --template=package
project1's packages.yaml does this:
dependancies:
project1:
path: ../project2
project2's packages.yaml does this:
flutter:
assets:
- lang/en.json
Which works and everything sees everything else and there is no complaint about that path for the asset and I've verified that it has exactly 2 spaces before assets: and exactly 4 actual spaces beofre - lang/en.json
The problem occurs when project2 tries to load lang/en.json like this in code form project2:
final jsonString =
await rootBundle.loadString('lang/en.json');
I get an "asset could not be loaded ${key}" on the loadString function.
if however I take exactly the same code and put it on project1 and copy the folder exactly and copy the exact same asset tag in packages.yaml, project1 has no problem loading the file. If I even leave the asset links on the project1 then project2 can load them just fine too.
Is this a bug, or am I doing something wrong with the package template version?
I have same issue. and I found the solution:
In project2, do some steps as below:
create assets folder
create lang folder(or any other folder name)
create json file in lang folder. Ex: en.json, vi.json...
in pubspec.yaml of project, you need to declare assets:
flutter:
assets:
- assets/lang/vi.json
- assets/lang/en.json
when using loadString then the path will be :
await rootBundle.loadString('packages/language_pack/assets/langen.json');
Note that packages is plural and language_pack is package name

Unable to load asset (file the app created)

Why does this cause a crash?
final directory = await getApplicationDocumentsDirectory();
final testFile = File("${directory.path}/test.txt");
await testFile.writeAsString("Hello there!", flush: true);
final ByteData bytes = await rootBundle.load(testFile.path);
The rootBundle.load() call causes:
Unhandled Exception: Unable to load asset: /data/user/0/com.flutter.tests/app_flutter/test.txt
BUT if I do a hot reload then it works fine until I restart the app.
I have a dependency path_provider: any in pubspec.yaml which is needed for the getApplicationDocumentsDirectory().
rootBundle only contains assets that were specified by pubspec.yaml and were built with your application. It's not supposed to access files created within file system. Use File or Image classes to open created files.
final bytes = testFile.readAsBytesSync();
The rootBundle contains the resources that were packaged with the
application when it was built. To add resources to the rootBundle for
your application, add them to the assets subsection of the flutter
section of your application's pubspec.yaml manifest.