Where to place json file inside my flutter iOS plugin

81 Views Asked by At

I try to create iOS plugin for flutter. For that I want to use a json file, but I haven't managed to read it yet.

I don't know if I've placed my json file correctly in my project :

  • At the root of ios folder
  • Inside Assets folder of ios folder
  • In assets folder of the root of my plugin project

Could you please explain me where to placed my json file.

I link you my function, in case I made a mistake in it.

Currently I call my loadSound function with packages/my_plugin/assets/sound.json filename.

Thanks for your help,

Best Regards,


struct Sound: Decodable {
    var id : String
    var title: String
    var uri: String
    var category: String

    var dictionary: [String: Any] {
        return [
            "id": id,
            "title": title,
            "uri": uri,
            "category": category
        ]
    }
    var nsDictionary: NSDictionary {
        return dictionary as NSDictionary
    }
}

private func loadSound(filename: String) -> [Sound] {
        if let url = Bundle.main.url(forResource: filename, withExtension: "json") {
            do {
                let data = try Data(contentsOf: url)
                let decoder = JSONDecoder()
                let sounds = try decoder.decode([Sound].self, from: data)
                return sounds
            } catch {
                print("Error loading JSON data: \(error)")
            }
        }
        return []
    }
0

There are 0 best solutions below