Unable to load local json file from assets in Flutter Package project

850 Views Asked by At

I was working on building a Package project in Flutter and hence I have to add lot of .svg image files and local config.json file from Assets for the Package. But I can able to load the .svg files, but it was throwing error for loading .json file.

I have filed a bug on the github flutter repo, please check the below link for further details. Please do the needful.

Unable to load local json file from "assets" folder in Flutter Package project

2

There are 2 best solutions below

0
Junsu Cho On
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  # assets:
  #  - images/a_dot_burr.jpeg
  #  - images/a_dot_ham.jpeg
  assets:
    - assets/JsonConfig/test.json

pubspec.yaml

1
adee alamsz On

Try to write the path with the following format packages/<yourpackagename>/<path to your file> such as below

String jsonString = await rootBundle.loadString('packages/yourpackagename/assets/yourfile.json');