Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Unable to load text assets

Tags:

flutter

dart

My file endpoints.json is in /WORKPLACE_DIR/assets/json/endpoints.json

{
  "key": "value"
}

I have updated my pubspec.yaml

assets:
    - assets/json/endpoints.json

In /tests/some_test.dart

I do

test('my test', () async {
      final endpoints = json.decode(await rootBundle.loadString("assets/json/endpoints.json"));
    }

It still gives an error

ERROR: Unable to load asset: assets/json/endpoints.json
package:flutter/src/services/asset_bundle.dart 221:7  PlatformAssetBundle.load
===== asynchronous gap ===========================
dart:async                                            _AsyncAwaitCompleter.completeError
package:flutter/src/services/asset_bundle.dart        PlatformAssetBundle.load
===== asynchronous gap ===========================
dart:async                                            _asyncThenWrapperHelper
package:flutter/src/services/asset_bundle.dart        PlatformAssetBundle.load
package:flutter/src/services/asset_bundle.dart 67:33  AssetBundle.loadString

I have tried flutter clean but it still gives this error. I am not sure what to do.

like image 669
Souradeep Nanda Avatar asked Sep 27 '18 09:09

Souradeep Nanda


People also ask

What is Assetimage in flutter?

A flutter app when built has both assets (resources) and code. Assets are available and deployed during runtime. The asset is a file that can include static data, configuration files, icons, and images. The Flutter app supports many image formats, such as JPEG, WebP, PNG, GIF, animated WebP/GIF, BMP, and WBMP.

How do I use rootBundle in flutter?

Applications have a rootBundle, which 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.


1 Answers

I had similar problem with images. The reason was simple, I skipped the tab before "assets:". If the words "flutter:" and "assets:" are on the same level in your pubspec.yaml file you will get the "unable to load asset" error. So just add the tab before "assets:" and "- assets/json/endpoints.json".

like image 85
AlexGirenko Avatar answered Oct 13 '22 22:10

AlexGirenko