Flutter - Format Exception : Unexpected end of input (at character 1)

Viewed 2228

I'm facing this Exception while loading json from a .json file. complete Exception is :

E/flutter ( 4062): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: 
FormatException: Unexpected end of input (at character 1)
E/flutter ( 4062): 
E/flutter ( 4062): ^
E/flutter ( 4062): 
E/flutter ( 4062): #0      _ChunkedJsonParser.fail (dart:convert-  patch/convert_patch.dart:1405:5)
E/flutter ( 4062): #1      _ChunkedJsonParser.close (dart:convert-patch/convert_patch.dart:523:7)
E/flutter ( 4062): #2      _parseJson (dart:convert-patch/convert_patch.dart:41:10)
E/flutter ( 4062): #3      JsonDecoder.convert (dart:convert/json.dart:506:36)
E/flutter ( 4062): #4      JsonCodec.decode (dart:convert/json.dart:157:41)
E/flutter ( 4062): #5      _HomePageState.loadJsonData.<anonymous closure>.<anonymous closure> (package:audio_player/presentation/pages/home_page.dart:22:35)
E/flutter ( 4062): #6      State.setState (package:flutter/src/widgets/framework.dart:1088:30)
E/flutter ( 4062): #7      _HomePageState.loadJsonData.<anonymous closure> (package:audio_player/presentation/pages/home_page.dart:20:27)
E/flutter ( 4062): #8      _rootRunUnary (dart:async/zone.dart:1436:47)
E/flutter ( 4062): #9      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 4062): <asynchronous suspension>
E/flutter ( 4062): #10     _HomePageState.loadJsonData (package:audio_player/presentation/pages /home_page.dart:19:5)
E/flutter ( 4062): <asynchronous suspension>

My Json file path (lib/utils/json/popular_books.json) and code look like this:

[
  {
   "rating":"4.5",
   "title":"sometitle",
   "text":"sometext",
   "img":"lib/utils/assets/image_1",
  }
   ...
]

My images are in the same folder utils/assets. I'm loading json

  List? popularBooks;

  loadJsonData() async {
    await DefaultAssetBundle.of(context).loadString("lib/utils/json/popular_books.json").then(
          (loadedJson) => setState(
            () {
              popularBooks = json.decode(loadedJson);
            },
          ),
        );
      }

  @override
  void initState() {
  loadJsonData();
  super.initState();
 }

And I'm trying to load only the images in the page view :

PageView.builder(
              itemCount: popularBooks==null?0:popularBooks!.length,
              controller: _controller,
              itemBuilder: (context, index) {
                return Container(
                  margin: EdgeInsets.only(right: 10),
                  height: 150,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                   ),
                   child: Image.asset("${popularBooks![index]["img"]}"),
                 );
               },
             ),

And then this Format Exception If you have any fix please share it I'd me very thankful. <3

1 Answers

Your JSON is invalid. With a json validator you can spot the problem. In your JSON it is the following syntax error:

[
  {
   "rating":"4.5",
   "title":"sometitle",
   "text":"sometext",
   "img":"lib/utils/assets/image_1", <-- This comma is the problem
  }
]
Related