1

In Flutter, no matter what json file i try to decode I get the same above titled error. Originally I thought it was something with my project but after starting a new template Flutter project I still receive the same error. I have the json file in my root folder and have added them to the pubspec.yaml file:

  assets:
      - Sample-JSON-data.json
      - Sample-employee-JSON-data.json

Main.dart code at the moment:

    var jsonString = 'Sample-JSON-data.json';
    var response = jsonDecode(jsonString);
    print(response);

I have verified my json data on multiple test sites as well as tried various methods in the Flutter Documentation. Json data:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    151.0763783444317,
                    -33.98045132346684
                ]
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    151.07774912725728,
                    -33.97470462237792
                ]
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    151.07774912725728,
                    -33.97470462237792
                ]
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    151.07774912725728,
                    -33.97470462237792
                ]
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    151.0763783444317,
                    -33.98045132346684
                ]
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    151.07774912725728,
                    -33.97470462237792
                ]
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    151.07774912725728,
                    -33.97470462237792
                ]
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    151.07774912725728,
                    -33.97470462237792
                ]
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    151.0763783444317,
                    -33.98045132346684
                ]
            }
        }
    ]
}
jkd359
  • 151
  • 4
  • 16

2 Answers2

2

jsonDecode expects an input to be valid JSON serialized into String fe. { "message": "Hello World!" }. So this would work: jsonDecode('{"message": "Hello World!"}').

What you are passing to it is a file name, it won't automatically read the file for you. You can check how to do that here:

Flutter - Read text file from assets

pr0gramist
  • 8,305
  • 2
  • 36
  • 48
1

Actually you are not loading the data from the json file.

Try:

var jsonString = await rootBundle.loadString('Sample-JSON-data.json')
Hemanth Raj
  • 32,555
  • 10
  • 92
  • 82
  • Thanks, I've used this previously and I must have a case of fuzzy-brain so thank god you brought it up. – jkd359 Mar 04 '20 at 13:48